[英]Why tkinter grid, columnconfigure and rowconfigure value not changing dynamically?
[英]columnconfigure and rowconfigure tkinter page frames
import tkinter as tk
class program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.title("Staff Management System")
self.geometry("1280x720")
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.frames = {}
#frame that will hold all the elements
container = tk.Frame(self)
container.grid(row=1,column=0,)
for i in range(128):
container.columnconfigure(i,weight=1)
for i in range(128):
container.rowconfigure(i,weight=1)
#listing frames (pages) to be controlled
for F in (homePage, staffLogin):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = "nsew")
self.show_frame(staffLogin)
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class homePage(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent,*args, **kwargs)
self.controller = controller
promotionsButton = tk.Button(self, text="Promotions", height = 4)
promotionsButton.grid(row=1, column = 128, sticky='w')
staffLoginButton = tk.Button(self, text="Staff Login", width = 50, height = 20)
staffLoginButton.grid(row=1, column=1)
managerLoginButton = tk.Button(self, text="Manager Login", width = 50, height = 20)
managerLoginButton.grid(row=1,column=2)
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
self.controller = controller
userIDLabel = tk.Label(self, text = "UserID:")
userIDInput = tk.Entry(self, width=50, font=("Helvectia",16))
userIDLabel.grid(sticky="W", row=67,column=59)
userIDInput.grid(row=67, column=60)
userPasswordLabel = tk.Label(self, text = "Password:")
userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16))
userPasswordLabel.grid(sticky="W", row=70, column=59)
userPasswordInput.grid(row=70, column=60)
loginButton = tk.Button(self, text="Login", height=2, width=7)
loginButton.grid(sticky="E",row = 71, column=60)
thing = program()
thing.mainloop()
我试图使用容器列和行配置方法来创建 128 个“框”,我可以为我的每个页面网格化小部件。 但是,当我运行程序时它不起作用,当我在不同页面中创建小部件时尝试传递“父”而不是“自我”时,我的 show_frame function 不起作用,而是显示我的两个小部件同时页面。
您在容器上调用rowconfigure
和columnconfigure
,但容器只有一行和一列用于保存每个“页面”。 您需要在每个页面而不是容器上调用这些方法。
class homePage():
def __init__(self, parent, controller, *args, **kwargs):
...
for i in range(128):
self.columnconfigure(i,weight=1)
for i in range(128):
self.rowconfigure(i,weight=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.