我正在使用tkinter创建一个登录/注册系统。当用户单击登录或注册时,我希望所有窗口小部件都消失,以便新窗口小部件显示在屏幕上,具体取决于他们是否单击了登录或注册。因此,如果他们单击登录,他们的用户名和密码将出现新的标签和文本框。问题是我正在使用.place(),而我所看到的教程大多使用pack_forget
或grid_forget
我的代码:
from tkinter import *
class Window:
def __init__(self, master):
root.title("Sign Up or Login")
root.minsize(width=300, height=300)
root.maxsize(width=300,height=300)
self.login_button = Button(master, text = "Login", width=18,height=4, command=self.LoginPage)
self.signup_button = Button(master, text = "Sign Up", width=18,height=4, command=self.SignupPage)
self.login_button.place(relx=0.5, rely=0.3, anchor=CENTER)
self.signup_button.place(relx=0.5, rely=0.7, anchor=CENTER)
def LoginPage(self):
root.title("Login")
def SignupPage(self):
root.title("Sign Up")
root = Tk()
run = Window(root)
root.mainloop()
我的界面:
答案 0 :(得分:0)
无论您使用place
,pack
还是grid
。最好的解决方案适用于所有人:
for widgets in root.winfo_children():
widgets.destory()
它循环遍历小部件并删除它们。您可以尝试:
from tkinter import *
class Window:
def __init__(self, master):
root.title("Sign Up or Login")
root.minsize(width=300, height=300)
root.maxsize(width=300,height=300)
self.login_button = Button(master, text = "Login", width=18,height=4, command=self.LoginPage)
self.signup_button = Button(master, text = "Sign Up", width=18,height=4, command=self.SignupPage)
self.login_button.place(relx=0.5, rely=0.3, anchor=CENTER)
self.signup_button.place(relx=0.5, rely=0.7, anchor=CENTER)
def LoginPage(self):
root.title("Login")
self.Restore()
def SignupPage(self):
root.title("Sign Up")
self.Restore()
def Restore(self):
for widgets in root.winfo_children():
widgets.destroy()
root = Tk()
run = Window(root)
root.mainloop()