缩放 tkinter 项目以适应 320x240 树莓派屏幕

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

实际结果

http://img.mukewang.com/63aa51e10001ec9106590373.jpg

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

预期结果

http://img2.mukewang.com/63aa51ef0001d1be04770358.jpg

我制作几何图形的原因320x240是因为我想在我的 Raspberry Pi 屏幕上运行这个 GUI 320x240。然而,pi 将 HDMI 电缆的输出镜像到屏幕。HDMI 输出1280x480。我只需要它在树莓派屏幕上看起来太清晰,不管它在 HDMI 输出上看起来有多长。

我试过的

我用过root.attributes('-fullscreen', True),认为这会缩放根框架的内容以匹配屏幕分辨率,但是这条线只会使 tkinter 窗口全尺寸。

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

redpoly2 图片

http://img2.mukewang.com/63aa52050001653e03190238.jpg

精慕HU
浏览 130回答 2
2回答

jeck猫

您可以在不使用小部件的情况下拥有背景图像Canvas,这样做将允许您使用 tkinter 的几何管理器来放置小部件。我不太明白Raspberry Pi 的320x240 屏幕和1280x480 HDMI 之间的关系。下面的代码说明了如何显示背景图像和它上面的一些小部件。还有一个Button可以在您想要的两个之间切换窗口的大小。from PIL import Image, ImageTktry:&nbsp; &nbsp; import Tkinter as tkexcept:&nbsp; &nbsp; import tkinter as tkpath_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):&nbsp; &nbsp; return "#%02x%02x%02x" % rgbdef change_size():&nbsp; &nbsp; """ Sets/changes window size to next one available in WIN_SIZES. """&nbsp; &nbsp; global cur_size&nbsp; &nbsp; cur_size = (cur_size + 1) % len(WIN_SIZES)&nbsp; &nbsp; config_window()def config_window():&nbsp; &nbsp; """ Sets root window's title, size, and background image. """&nbsp; &nbsp; global background_label&nbsp; &nbsp; geometry = '{}x{}'.format(*WIN_SIZES[cur_size])&nbsp; &nbsp; root.geometry(geometry)&nbsp; &nbsp; root.title(geometry)&nbsp; &nbsp; # Resize background to fit window size.&nbsp; &nbsp; btn_img = background_image.resize(WIN_SIZES[cur_size], resample=Image.BICUBIC)&nbsp; &nbsp; btn_img = ImageTk.PhotoImage(btn_img)&nbsp; # Make tkinter compatible.&nbsp; &nbsp; if not background_label:&nbsp; # Create Label if necessary.&nbsp; &nbsp; &nbsp; &nbsp; background_label = tk.Label(root)&nbsp; &nbsp; background_label.config(image=btn_img)&nbsp; &nbsp; background_label.image = btn_img&nbsp; # Keep reference.&nbsp; &nbsp; background_label.place(x=0, y=0, relwidth=1, relheight=1)root = tk.Tk()background_image = Image.open(path_to_bkgr_img)background_label = Nonecur_size = 0config_window()titleLabel = tk.Label(root, fg="white", text="TEXT", borderwidth=2, relief="solid",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; borderwidth=2, relief="raised", bg=_from_rgb((239, 36, 37)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font=("Courier", 22))logButton.pack(padx=5, pady=5, expand=1)root.bind_all('<KeyPress-Escape>', lambda *event: quit())&nbsp; # Press Esc key to quit app.root.mainloop()以下是显示每种尺寸所显示内容的屏幕截图:

翻过高山走不出你

RPi 的输出可以在config.txt/boot 分区上的文件中配置。通过参考config.txt 视频页面,您可以将 HDMI 的输出设置为特定模式。在您的情况下,这可能需要在 raspberry pi 论坛中描述的自定义设置。config.txt您使用以下配置字符串指定新模式:hdmi_cvt=<width> <height> <framerate> <aspect> <margins> <interlace> <rb>在哪里:Value&nbsp; &nbsp; &nbsp; &nbsp;Default&nbsp; &nbsp; &nbsp;Descriptionwidth&nbsp; &nbsp; &nbsp; &nbsp;(required)&nbsp; width in pixelsheight&nbsp; &nbsp; &nbsp; (required)&nbsp; height in pixelsframerate&nbsp; &nbsp;(required)&nbsp; framerate in Hzaspect&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aspect ratio 1=4:3, 2=14:9, 3=16:9, 4=5:4, 5=16:10, 6=15:9margins&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0=margins disabled, 1=margins enabledinterlace&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0=progressive, 1=interlacedrb&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0=normal, 1=reduced blanking
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python