我有一个Class层次结构,我试图在保留特定属性(例如窗口名称,尺寸,可调整大小等)的同时切换tkinter窗口。我对可调整大小的部分有一些问题,因为它有两个值:
import tkinter as tk
from tkinter import font as tkfont
class Manifold(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self) #container = stack of frames; one on top is visible
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F,geometry,title in zip((StartPage,PageOne,PageTwo,PageThree),
("532x279","532x279","254x279","299x620"),
("","Experimental Data","Orientation Distribution","Manifold Embedding"),
((False,False),(False,False),(True,True),(True,True))):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = (frame,geometry,title,(option1,option2)) #puts all pages in stacked order
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, geometry, title = self.frames[page_name]
self.update_idletasks()
self.geometry(geometry) #changes between window sizes
tk.Tk.wm_title(self, title) #window heading
###UNKNOWN:
self.resizable(*options)
###########
frame.tkraise() #raises window to top
if __name__ == "__main__":
app = Manifold()
app.mainloop()
非常感谢任何建议。
答案 0 :(得分:0)
关闭。试试这个:
self.frames = {}
for F,geometry,title,options in zip((StartPage,PageOne,PageTwo,PageThree),
("532x279","532x279","254x279","299x620"),
("","Experimental Data","Orientation Distribution","Manifold Embedding"),
((False,False),(False,False),(True,True),(True,True))):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = (frame,geometry,title,options) #puts all pages in stacked order
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, geometry, title,options = self.frames[page_name]
self.geometry(geometry) #changes between window sizes
self.title(title) #window heading
self.resizable(*options)
###########
frame.tkraise() #raises window to top
假设这是Tk()
的子类。
update_idletasks
(它很少使用)。
答案 1 :(得分:0)
将这些选项放在每个Frame的import tkinter as tk
class BigWindow(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
tk.Button(self, text='go to\nsmall window', command=lambda: master.show_frame(SmallWindow)).pack()
def tkraise(self):
self.master.title('Big Window')
self.master.geometry('600x600')
self.master.resizable(True, True)
tk.Frame.tkraise(self) # raise this Frame
class SmallWindow(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
tk.Button(self, text='go to\nbig window', command=lambda: master.show_frame(BigWindow)).pack()
def tkraise(self):
self.master.title('Small Window')
self.master.geometry('200x200')
self.master.resizable(False, False)
tk.Frame.tkraise(self)
class Manifold(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frames = {}
for F in (SmallWindow, BigWindow):
frame = F(self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(SmallWindow)
def show_frame(self, page): #show a frame for the given page
self.frames[page].tkraise()
if __name__ == "__main__":
app = Manifold()
app.mainloop()
方法中会更加简洁。为了做到这一点,你需要一个挂钩到根(Tk()实例)。既然你已经对你的“容器”框架(为什么?)进行了一些混淆,你需要确保传递根实例。一个简单的例子:
import tkinter as tk
class AutoSizeFrame(tk.Frame):
def tkraise(self):
self.master.title(self.title)
self.master.geometry(self.geometry)
self.master.resizable(*self.resizemodes)
tk.Frame.tkraise(self) # raise this Frame
class BigWindow(AutoSizeFrame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.title = 'Big Window'
self.geometry = '600x600'
self.resizemodes = (True, True)
tk.Button(self, text='go to\nsmall window', command=lambda: master.show_frame(SmallWindow)).pack()
class SmallWindow(AutoSizeFrame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.title = 'Small Window'
self.geometry = '200x200'
self.resizemodes = (False, False)
tk.Button(self, text='go to\nbig window', command=lambda: master.show_frame(BigWindow)).pack()
class Manifold(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frames = {}
for F in (SmallWindow, BigWindow):
frame = F(self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(SmallWindow)
def show_frame(self, page): #show a frame for the given page
self.frames[page].tkraise()
if __name__ == "__main__":
app = Manifold()
app.mainloop()
你可以通过为所有其他帧继承基类,然后只设置一些变量来使这个更清晰:
{{1}}