我一直在尝试制作一个数据输入应用程序,该应用程序的页面可以输入新的类别,马匹等。每个页面都有不同的页面,但是问题是它们一次全部被调用,而不是在请求时一个接一个地被调用。当我运行它时,我会看到一个窗口,其中包含我已打包的所有内容,并且按钮没有任何响应。
这是我的代码:
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()
答案 0 :(得分:1)
您复制的代码对框架使用grid
,而不对pack
。该代码仅适用于grid
用于页面。