[英]TKinter GUI resizing window dynamically with background and buttons
再次需要你们的帮助,构建一个 GUI 项目并需要动态地制作它,以便当我更改窗口大小并适应新的窗口大小时,按钮、标签、输入框、背景和所有内容都会调整大小。
到目前为止,这是仅用于调整工作背景大小的代码,但是有带有标签、按钮等的主屏幕。此外,我需要添加更多 8 个按钮,每个按钮都可以打开新窗口,在我将拥有的新窗口上使它们也动态...这里是代码示例:
from tkinter import *
from PIL import Image, ImageTk
class MainScreen(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.configure(background="black")
self.image = Image.open("bg.jpg")
# label for the background image
self.background = Label(self)
self.background.place(x=0, y=0)
self.bind('<Configure>', self._resize_image)
# Label 1 welcome message
Label(root, text="Welcome", bg="#12355F", fg="white",
font="tahoma 12 bold") .place(x=10, y=10)
# Label 2
Label(root, text="Add:", bg="#164883", fg="white",
font="tahoma 10 bold").place(x=10, y=80)
# Add Button + New Window Open
def openNewWindow():
def close_window():
newWindow.destroy()
newWindow = Toplevel(master)
newWindow.title("New Window")
window_height = 565
window_width = 970
screen_width = newWindow.winfo_screenwidth()
screen_height = newWindow.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
newWindow.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
newWindow.configure(background="#071530")
# create a text box
output = Text(newWindow, width=75, height=6, wrap=WORD, background="white")
output.grid(row=2, column=0, columnspan=10, sticky=W, padx=170, pady=30)
# create lable
Label(newWindow, text="BLABLA", bg="#071530", fg="white", font="calibre 20 bold").grid(row=0,
column=0,
sticky=W,
padx=340,
pady=30)
Label(newWindow, text="Subtext.",
bg="black", fg="white", font="calibre 12 bold").grid(row=1, column=0, sticky=W, padx=230, pady=10)
# create lable
Label(newWindow, text="CLICK", bg="black", fg="white", font="calibre 12 bold").grid(row=3,
column=0,
sticky=W,
padx=320,
pady=10)
Label(newWindow, text="EXIT", bg="black", fg="white", font="calibre 12 bold").grid(row=3,
column=0,
sticky=W,
padx=550,
pady=10)
# create a button
Button(newWindow, text="Exit", width=6, command=close_window, bg="orange").grid(row=4, column=0, sticky=W,
padx=570, pady=1)
Button(newWindow, text="View", width=6, bg="orange").grid(row=4, column=0, sticky=W, padx=350, pady=1)
newWindow.mainloop()
# button 1 main menu = ADD
self.button = Button(self, text="Add", width=4, bg="orange", command=openNewWindow)
self.button.place(x=220, y=79.4)
def _resize_image(self,event):
if event.widget is self:
# resize background image to fit the frame size
image = self.image.resize((event.width, event.height))
self.background_image = ImageTk.PhotoImage(image)
self.background.configure(image=self.background_image)
root = Tk()
root.title("GUI")
window_height = 565
window_width = 970
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
e = MainScreen(root)
e.pack(fill=BOTH, expand=1)
root.mainloop()
您可以通过调用当前帧的 grid_remove() 然后在新帧上调用 grid() 来完成此操作。 或者,您可以懒惰地对所有内容调用 grid_remove(),这样您就不必记住哪个页面是当前页面。
def _resize_image(self,event)::
'''Show a frame for the given page name'''
for frame in self.frames.values():
frame.grid_remove()
frame = self.frames[page_name]
frame.grid()
注意:如果您使用根窗口上的几何方法为主窗口指定固定大小,或者用户手动调整窗口大小,则自动调整大小将停止工作。 这是因为 tkinter 假设如果某些内容明确请求窗口大小,则应该遵守该大小。
如果您总是希望窗口调整大小,则应将几何图形重置为空字符串。 您可以将其添加为 show_frame 方法中的最后一条语句:
frame.winfo_toplevel().geometry("")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.