我有一个问题,在tkinter中彼此堆叠'Pages'。在tkinter中彼此不堆叠的帧
我有一个主要的Frame
包含两个子帧,其中包含不同的信息。第一个子帧包含一个Listbox
和一对按钮,并打包在主框架的左侧。第二帧应该包含不同的'页面'(现在两个),并让它们填满整个框架。 我的问题是,两个'页'并排显示,而不是彼此顶部。
import tkinter as tk
class Settings(tk.Tk):
def __init__(self, master=None):
tk.Tk.__init__(self, master)
self.focus_force()
self.grab_set()
# set focus to settings window
# Main window title
self.title("Settings")
# set up grid containers
container_main = tk.Frame(self, width=500, height=700)
container_main.pack(side='top', fill='both', expand=True)
container_main.grid_rowconfigure(0, weight=1)
container_main.grid_columnconfigure(0, weight=1)
container_listbox = tk.Frame(container_main, bg='blue', width=200, height=700)
container_listbox.pack(side='left', fill='both', expand=True)
container_listbox.grid_rowconfigure(0, weight=1)
container_listbox.grid_columnconfigure(0, weight=1)
container_settings = tk.Frame(container_main, bg='red', width=300, height=700)
container_settings.pack(side='right', fill='both', expand=True)
container_settings.grid_rowconfigure(0, weight=1)
container_settings.grid_columnconfigure(0, weight=1)
# build settings pages
self.frames = {}
self.frames["Options"] = Options(parent=container_listbox, controller=self)
self.frames["General"] = General(parent=container_settings, controller=self)
self.frames["Future"] = Future(parent=container_settings, controller=self)
如果我发现这两条线。我得到一个错误,说我不能在里面使用几何管理器网格。
# self.frames["General"].grid(row=0, column=0, sticky='nsew')
# self.frames["Future"].grid(row=0, column=0, sticky='nsew')
。
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class Options(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='List Box')
label.grid(row=0, column=0, sticky='nsew', padx=1, pady=1)
button1 = tk.Button(parent, text='General', command=lambda: controller.show_frame('General'))
button2 = tk.Button(parent, text='Future', command=lambda: controller.show_frame('Future'))
button1.grid(row=1, column=0, sticky='ew')
button2.grid(row=2, column=0, sticky='ew')
class General(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='General')
label.pack(side='left', fill='both', expand=True,)
print("Hi I'm General")
class Future(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='Future')
label.pack(side='left', fill='both', expand=True)
print("Hi I'm Future")
app = Settings()
app.mainloop()
这两个'页'被初始化并显示在同一时间,这是有道理的。因为frame.tkraise()
应该是这样做,但不是这样,我只是不知道如何使其中一个上升。我还希望能够在页面上或页面上做grid_forget()
,以避免将来可能意外地将值输入到隐藏的输入框中。
编辑:如果我注释掉'未来'页面,那么'一般'页面将占用整个框架空间,因此使用grid_forget()
我会得到相同的结果。我只是不知道我会在哪里,但grid_forget()
,然后还会在哪里重新配置或拨打grid()
?
解决'不能使用几何管理器网格内'问题。我仍然需要一次只显示一个'Page',例如[Bryan Oakley's](https://stackoverflow.com/a/7557028/7703610)示例减去for循环。 –
您的第一个陈述略有误导。您可以在窗口中同时使用包和网格,而不仅仅是共享共同父项的小部件。您的声明几乎使听起来像是您在窗口中的任何位置使用包装,然后您无法在任何地方使用网格,这是错误的。 –
这可能是你如何在框架上使用包。 –