jeck猫
您可以在不使用小部件的情况下拥有背景图像Canvas,这样做将允许您使用 tkinter 的几何管理器来放置小部件。我不太明白Raspberry Pi 的320x240 屏幕和1280x480 HDMI 之间的关系。下面的代码说明了如何显示背景图像和它上面的一些小部件。还有一个Button可以在您想要的两个之间切换窗口的大小。from PIL import Image, ImageTktry: import Tkinter as tkexcept: 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): return "#%02x%02x%02x" % rgbdef 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 = Nonecur_size = 0config_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()以下是显示每种尺寸所显示内容的屏幕截图: