[英]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.