问题描述
再次需要您的帮助,构建一个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",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",font="calibre 20 bold").grid(row=0,padx=340,pady=30)
Label(newWindow,text="Subtext.",bg="black",font="calibre 12 bold").grid(row=1,padx=230,pady=10)
# create lable
Label(newWindow,text="CLICK",font="calibre 12 bold").grid(row=3,padx=320,pady=10)
Label(newWindow,text="EXIT",padx=550,pady=10)
# create a button
Button(newWindow,text="Exit",width=6,command=close_window,bg="orange").grid(row=4,padx=570,pady=1)
Button(newWindow,text="View",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,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("")