Python 扩展tkinter项目以适应320x240 raspberry Pi屏幕

Python 扩展tkinter项目以适应320x240 raspberry Pi屏幕,python,tkinter,canvas,raspberry-pi,resolution,Python,Tkinter,Canvas,Raspberry Pi,Resolution,我正在开发一个tkinter GUI,它使用一个画布小部件,以便在背景中有一个图像和上面的小部件。这个GUI将在320x240raspberry pi屏幕上运行。我对为这些屏幕设计GUI很陌生,过去只为笔记本电脑开发过。当前,GUI如下所示: 实际结果 如你所见,它太小了。我想要的是: 预期结果 #!/usr/bin/env python try: import Tkinter as tk except: import tkinter as tk from PIL import

我正在开发一个tkinter GUI,它使用一个画布小部件,以便在背景中有一个图像和上面的小部件。这个GUI将在
320x240
raspberry pi屏幕上运行。我对为这些屏幕设计GUI很陌生,过去只为笔记本电脑开发过。当前,GUI如下所示:

实际结果

如你所见,它太小了。我想要的是:

预期结果

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("1280x480")

#Define Canvas
canvas = tk.Canvas(root, width=320, height=240)
canvas.grid(row=1,column=1)


# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

# Called when user presses View Log button
def viewLogRaise():
    #Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    quote = """HAMLET: To be, or not to be--that is the question:
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune
    Or to take arms against a sea of troubles
    And by opposing end them. To die, to sleep--
    No more--and by a sleep to say we end
    The heartache, and the thousand natural shocks
    That flesh is heir to. 'Tis a consummation
    Devoutly to be wished."""
    logText.insert(tk.END, quote)

def backToMenu():
    #Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")

# Background
pathToGif = "redpoly2.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root,fg="white", text="TEXT",borderwidth=2,relief="solid", bg=_from_rgb((239, 36, 37)), font=("Courier", 44))
titleLabelWindow = canvas.create_window(160,90,window=titleLabel)
logButton = tk.Button(root,fg="white",text="View Log",command=viewLogRaise,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)), font=("Courier", 22))
logButtonWindow = canvas.create_window(160,180,window=logButton)
backButton = tk.Button(root,fg="white",text="Back",command=backToMenu,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)))
backButtonWindow = canvas.create_window(20,227,window=backButton)
canvas.itemconfigure(backButtonWindow, state="hidden")
logText=tk.Text(root,bg="white",height=12,width=35,borderwidth=2,relief="solid")
logTextWindow = canvas.create_window(160,110,window=logText)
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()

我之所以制作几何图形是因为我想在我的Raspberry Pi屏幕上运行这个GUI,它是
320x240
。然而,pi将HDMI电缆的输出镜像到屏幕上。HDMI输出
1280x480
。我只需要它在raspberry pi屏幕上看起来更清晰,无论它在HDMI输出上看起来有多长。

代码

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("1280x480")

#Define Canvas
canvas = tk.Canvas(root, width=320, height=240)
canvas.grid(row=1,column=1)


# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

# Called when user presses View Log button
def viewLogRaise():
    #Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    quote = """HAMLET: To be, or not to be--that is the question:
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune
    Or to take arms against a sea of troubles
    And by opposing end them. To die, to sleep--
    No more--and by a sleep to say we end
    The heartache, and the thousand natural shocks
    That flesh is heir to. 'Tis a consummation
    Devoutly to be wished."""
    logText.insert(tk.END, quote)

def backToMenu():
    #Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")

# Background
pathToGif = "redpoly2.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root,fg="white", text="TEXT",borderwidth=2,relief="solid", bg=_from_rgb((239, 36, 37)), font=("Courier", 44))
titleLabelWindow = canvas.create_window(160,90,window=titleLabel)
logButton = tk.Button(root,fg="white",text="View Log",command=viewLogRaise,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)), font=("Courier", 22))
logButtonWindow = canvas.create_window(160,180,window=logButton)
backButton = tk.Button(root,fg="white",text="Back",command=backToMenu,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)))
backButtonWindow = canvas.create_window(20,227,window=backButton)
canvas.itemconfigure(backButtonWindow, state="hidden")
logText=tk.Text(root,bg="white",height=12,width=35,borderwidth=2,relief="solid")
logTextWindow = canvas.create_window(160,110,window=logText)
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()
我尝试的

我使用了
root.attributes('-fullscreen',True)
,认为这将缩放根帧的内容以匹配屏幕分辨率,但是这一行只会使tkinter窗口完全变大

我考虑调整整个GUI的大小,使其在
1280x480
上运行,但这意味着它们的像素太多,pi屏幕无法显示

redpoly2图像

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("1280x480")

#Define Canvas
canvas = tk.Canvas(root, width=320, height=240)
canvas.grid(row=1,column=1)


# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

# Called when user presses View Log button
def viewLogRaise():
    #Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    quote = """HAMLET: To be, or not to be--that is the question:
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune
    Or to take arms against a sea of troubles
    And by opposing end them. To die, to sleep--
    No more--and by a sleep to say we end
    The heartache, and the thousand natural shocks
    That flesh is heir to. 'Tis a consummation
    Devoutly to be wished."""
    logText.insert(tk.END, quote)

def backToMenu():
    #Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")

# Background
pathToGif = "redpoly2.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root,fg="white", text="TEXT",borderwidth=2,relief="solid", bg=_from_rgb((239, 36, 37)), font=("Courier", 44))
titleLabelWindow = canvas.create_window(160,90,window=titleLabel)
logButton = tk.Button(root,fg="white",text="View Log",command=viewLogRaise,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)), font=("Courier", 22))
logButtonWindow = canvas.create_window(160,180,window=logButton)
backButton = tk.Button(root,fg="white",text="Back",command=backToMenu,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)))
backButtonWindow = canvas.create_window(20,227,window=backButton)
canvas.itemconfigure(backButtonWindow, state="hidden")
logText=tk.Text(root,bg="white",height=12,width=35,borderwidth=2,relief="solid")
logTextWindow = canvas.create_window(160,110,window=logText)
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()

您可以在不使用
画布
小部件的情况下获得背景图像,这样做将允许您使用tkinter的几何管理器来放置小部件。我真的不明白Raspberry Pi的320x240屏幕和1280x480 HDMI屏幕之间的关系

下面的代码演示了如何显示背景图像以及上面的一些小部件。还有一个
按钮
,用于在所需的两个窗口之间切换窗口大小

from PIL import Image, ImageTk
try:
    import Tkinter as tk
except:
    import tkinter as tk


path_to_bkgr_img = "redpoly2.jpg"
WIN_SIZES = (320, 240), (1280, 480)


# Translates an rgb tuple of int to a tkinter friendly color code.
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

def change_size():
    """ Sets/changes window size to next one available in WIN_SIZES. """
    global cur_size
    cur_size = (cur_size + 1) % len(WIN_SIZES)
    config_window()

def config_window():
    """ Sets root window's title, size, and background image. """
    global background_label

    geometry = '{}x{}'.format(*WIN_SIZES[cur_size])
    root.geometry(geometry)
    root.title(geometry)

    # Resize background to fit window size.
    btn_img = background_image.resize(WIN_SIZES[cur_size], resample=Image.BICUBIC)
    btn_img = ImageTk.PhotoImage(btn_img)  # Make tkinter compatible.

    if not background_label:  # Create Label if necessary.
        background_label = tk.Label(root)
    background_label.config(image=btn_img)
    background_label.image = btn_img  # Keep reference.
    background_label.place(x=0, y=0, relwidth=1, relheight=1)


root = tk.Tk()
background_image = Image.open(path_to_bkgr_img)
background_label = None
cur_size = 0
config_window()

titleLabel = tk.Label(root, fg="white", text="TEXT", borderwidth=2, relief="solid",
                      bg=_from_rgb((239, 36, 37)), font=("Courier", 44))
titleLabel.pack(padx=5, pady=5, expand=1)

logButton = tk.Button(root, fg="white", text="Change Size", command=change_size,
                      borderwidth=2, relief="raised", bg=_from_rgb((239, 36, 37)),
                      font=("Courier", 22))
logButton.pack(padx=5, pady=5, expand=1)

root.bind_all('<KeyPress-Escape>', lambda *event: quit())  # Press Esc key to quit app.
root.mainloop()
从PIL导入图像,ImageTk
尝试:
将Tkinter作为tk导入
除:
将tkinter作为tk导入
路径\u至\u bkgr\u img=“redpoly2.jpg”
WIN_大小=(320240),(1280480)
#将int的rgb元组转换为tkinter友好的颜色代码。
来自rgb(rgb)的定义:
返回“#%02x%02x%02x”%rgb
def更改_大小():
“”“将窗口大小设置/更改为WIN\u大小中可用的下一个窗口大小。”“”
全局电流大小
当前大小=(当前大小+1)%len(赢大小)
配置窗口()
def config_窗口():
“”“设置根窗口的标题、大小和背景图像。”“”
全局背景标签
几何体='{}x{}'。格式(*WIN_size[cur_size])
根。几何体(几何体)
根标题(几何)
#调整背景大小以适应窗口大小。
btn\u img=background\u image.resize(WIN\u size[cur\u size],resample=image.BICUBIC)
btn_img=ImageTk.PhotoImage(btn_img)#使tkinter兼容。
如果不是背景标签:#必要时创建标签。
背景\标签=tk.label(根)
background\u label.config(image=btn\u img)
background_label.image=btn_img#保留参考。
背景标签位置(x=0,y=0,relwidth=1,relheight=1)
root=tk.tk()
背景\u图像=图像。打开(路径\u到\u bkgr\u img)
背景标签=无
电流大小=0
配置窗口()
titleLabel=tk.Label(根,fg=“白色”,text=“文本”,borderwidth=2,relief=“实心”,
bg=_from_rgb((239,36,37)),字体=(“Courier”,44))
titleLabel.pack(padx=5,pady=5,expand=1)
logButton=tk.Button(root,fg=“white”,text=“Change Size”,command=Change\u Size,
borderwidth=2,relief=“raised”,bg=_from_rgb((239,36,37)),
字体=(“信使”,22))
logButton.pack(padx=5,pady=5,expand=1)
root.bind_all(“”,lambda*事件:quit())35;按Esc键退出应用程序。
root.mainloop()
以下是屏幕截图,显示了每种尺寸显示的内容:


可以在/boot分区上的
config.txt
文件中配置RPi的输出。通过参考,您可以将HDMI的输出设置为特定模式。在您的情况下,这可能需要所述的自定义设置

使用以下配置字符串在
config.txt
中指定新模式:

hdmi\u cvt=

其中:

值默认描述
宽度(必需)以像素为单位的宽度
高度(必需)以像素为单位的高度
帧速率(必需)帧速率,单位为Hz
纵横比1=4:3,2=14:9,3=16:9,4=5:4,5=16:10,6=15:9
边距0 0=禁用边距,1=启用边距
隔行0 0=逐行扫描,1=隔行扫描
rb 0 0=正常,1=减少消隐

您的问题不清楚-我不确定您想知道什么。我也无法运行您的代码,因为您没有提供
redpoly2.jpg
图像。您使用
create\u window
而不是使用
pack
和/或
grid
,有什么原因吗?通常,
pack
grid
在根据窗口大小扩展UI方面做得非常好。如果您将窗口显式放置在特定坐标处,则您完全有责任处理分辨率差异。@BryanOakley我使用画布是因为我想要背景图像。我找不到将图像分配给帧的方法,因为帧没有图像属性。@martineau我提供了图像。请原谅我的无知,我不熟悉raspberry pi及其“常规”输出与HDMI。不管怎样,如果希望GUI适合320x240,为什么要设置
root.geometry(“1280x480”)