[英]Tkinter shows all pages at once rather than one by one
I have been trying to make a data entry application, that has pages to enter new categories, horses, etc. I have different pages for each but the problem is they are all called at once rather than one by one upon request.我一直在尝试制作一个数据输入应用程序,该应用程序具有用于输入新类别、马匹等的页面。每个页面都有不同的页面,但问题是它们都是一次性调用的,而不是根据请求一个一个调用。 When I run it I get a window with all the things I have packed and the buttons are not responding to anything.当我运行它时,我会看到一个窗口,里面有我打包的所有东西,但按钮没有任何响应。
Here is my code:这是我的代码:
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (MainMenu, AddData, AddHorse, AddCategory):
frame = F(container, self)
self.frames[F] = frame
frame.pack()
self.show_frame(MainMenu)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MainMenu(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
add_category_btn = tk.Button(self, text="Add Category",
command=lambda: controller.show_frame(AddCategory))
add_horse_btn = tk.Button(self, text="Add Horse",
command=lambda: controller.show_frame(AddHorse))
add_data_btn = tk.Button(self, text="Add Data",
command=lambda: controller.show_frame(AddData))
add_category_btn.pack()
add_horse_btn.pack()
add_data_btn.pack()
class AddCategory(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='Add Category')
label.pack()
home_button = tk.Button(self, text='Go Home',
command=lambda: controller.show_frame(MainMenu))
home_button.pack()
class AddHorse(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='Add Horse')
label.pack()
home_button = tk.Button(self, text='Go Home',
command=lambda: controller.show_frame(MainMenu))
home_button.pack()
class AddData(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='Add Data')
label.pack()
home_button = tk.Button(self, text='Go Home',
command=lambda: controller.show_frame(MainMenu))
home_button.pack()
app = App()
app.mainloop()
The code you copied uses grid
, not pack
for the frames.您复制的代码使用grid
,而不是为框架pack
。 This code only works with grid
for the pages.此代码仅适用于页面的grid
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.