缩放 tkinter 项目以适应 320x240 raspberry Pi 屏幕
Scaling a tkinter project to fit 320x240 raspberry Pi screen
我正在开发一个 tkinter GUI,它利用 canvas 小部件以便在背景中显示图像,而小部件 above.This GUI 将 运行 在 320x240
raspberry pi screen.I 是为这些屏幕设计 GUI 的新手,过去只为笔记本电脑开发过。当前 GUI 如下所示:
实际结果
如您所见,它太小了。我想要的是:
预期结果
我制作几何图形 320x240
的原因是因为我想 运行 这个 GUI 在我的 Raspberry Pi 屏幕上 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 window 全尺寸。
我想在 1280x480
上将整个 GUI 的大小调整为 运行,但这意味着它们对于 pi 屏幕来说像素太多而无法显示。
redpoly2 图片
您可以在不使用 Canvas
小部件的情况下拥有背景图像,这样做将允许您使用 tkinter 的几何管理器来放置小部件。我不太明白 Raspberry Pi 的 320x240 屏幕和 1280x480 HDMI 屏幕之间的关系。
下面的代码说明了如何显示背景图像和它上面的一些小部件。还有一个 Button
可以在您想要的两者之间切换 window 的大小。
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()
以下是显示每种尺寸所显示内容的屏幕截图:
RPi 的输出可以在 /boot 分区上的 config.txt
文件中配置。通过引用 the config.txt video page you can set the output of the HDMI to a specific mode. In your case this may require custom settings which are described here within the raspberry pi forum。
您在 config.txt
中使用以下配置字符串指定新模式:
hdmi_cvt=<width> <height> <framerate> <aspect> <margins> <interlace> <rb>
其中:
Value Default Description
width (required) width in pixels
height (required) height in pixels
framerate (required) framerate in Hz
aspect 3 aspect ratio 1=4:3, 2=14:9, 3=16:9, 4=5:4, 5=16:10, 6=15:9
margins 0 0=margins disabled, 1=margins enabled
interlace 0 0=progressive, 1=interlaced
rb 0 0=normal, 1=reduced blanking
我正在开发一个 tkinter GUI,它利用 canvas 小部件以便在背景中显示图像,而小部件 above.This GUI 将 运行 在 320x240
raspberry pi screen.I 是为这些屏幕设计 GUI 的新手,过去只为笔记本电脑开发过。当前 GUI 如下所示:
实际结果
如您所见,它太小了。我想要的是:
预期结果
我制作几何图形 320x240
的原因是因为我想 运行 这个 GUI 在我的 Raspberry Pi 屏幕上 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 window 全尺寸。
我想在 1280x480
上将整个 GUI 的大小调整为 运行,但这意味着它们对于 pi 屏幕来说像素太多而无法显示。
redpoly2 图片
您可以在不使用 Canvas
小部件的情况下拥有背景图像,这样做将允许您使用 tkinter 的几何管理器来放置小部件。我不太明白 Raspberry Pi 的 320x240 屏幕和 1280x480 HDMI 屏幕之间的关系。
下面的代码说明了如何显示背景图像和它上面的一些小部件。还有一个 Button
可以在您想要的两者之间切换 window 的大小。
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()
以下是显示每种尺寸所显示内容的屏幕截图:
RPi 的输出可以在 /boot 分区上的 config.txt
文件中配置。通过引用 the config.txt video page you can set the output of the HDMI to a specific mode. In your case this may require custom settings which are described here within the raspberry pi forum。
您在 config.txt
中使用以下配置字符串指定新模式:
hdmi_cvt=<width> <height> <framerate> <aspect> <margins> <interlace> <rb>
其中:
Value Default Description
width (required) width in pixels
height (required) height in pixels
framerate (required) framerate in Hz
aspect 3 aspect ratio 1=4:3, 2=14:9, 3=16:9, 4=5:4, 5=16:10, 6=15:9
margins 0 0=margins disabled, 1=margins enabled
interlace 0 0=progressive, 1=interlaced
rb 0 0=normal, 1=reduced blanking