Python 调整帧大小
我已经被困了几天,试图弄明白如何使用这种方法在TKInter中动态调整帧的大小Python 调整帧大小,python,user-interface,tkinter,Python,User Interface,Tkinter,我已经被困了几天,试图弄明白如何使用这种方法在TKInter中动态调整帧的大小 class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) # the container is where we'll stack a bunch of frames # on top of each other, t
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
我从中复制了这段代码,因为我采用了相同的方法
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
我面临的问题是,使用这种方法,所有帧都堆叠在一个容器中,而这个容器的大小是其“最大帧”的大小。从一个帧移动到另一个帧不会动态调整相应窗口的大小,并且会在小帧中产生巨大的可用空间。我尝试了不同的技术,使容器中的所有帧都可以动态调整大小,但没有取得多大成功。有人能建议我能做什么吗?不要堆叠框架,而是确保网格一次只管理一个框架。您可以通过调用当前帧的
grid\u remove()
,然后在新帧上调用grid()
。或者,由于懒惰,您可以对所有内容调用grid\u remove()
,这样您就不必记住哪个页面是当前页面
def show_frame(self, page_name):
'''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\u frame
方法中的最后一条语句:
frame.winfo_toplevel().geometry("")
成功了!非常感谢,布莱恩。但是,我正在考虑分别设置每个帧的窗口大小。比如frame1-300x300,frame2-400x550等等。我有什么办法可以做到吗?事实上,这比我想象的要容易。show_框架中很少有if语句起作用。@user134:最简单的方法是在每个框架上将
overrideredirect
设置为false。这样,您就可以按常规方式直接为框架指定宽度和高度。这通常不是最好的方法。相反,将内部小部件配置为您想要的大小,并让框架增大或缩小以容纳小部件。如果显式设置宽度和高度,如果用户更改字体,或者UI运行在具有不同分辨率的系统上,则UI可能无法正常工作。@BryanOakley据您所知,是否有方法使此转换看起来更平滑?虽然从功能上讲,它完成了任务,但我发现在删除当前帧然后添加新帧的过程中出现了一些闪光,这是不吸引人的。我尝试使用winfo_reqwidth/reqheight属性,但在设置几何体和重新调整小部件时,加载过程中似乎有很多延迟。