Python 单击按钮后删除所有小部件?
我正在使用tkinter创建登录/注册系统。当用户单击登录或注册时,我希望所有小部件消失,新的小部件显示在屏幕上,具体取决于他们是单击登录还是注册。因此,如果他们点击登录,新的标签和文本框将出现他们的用户名和密码。问题是我使用的是.place(),我看到的教程大多使用的是Python 单击按钮后删除所有小部件?,python,python-3.x,tkinter,tkinter-layout,Python,Python 3.x,Tkinter,Tkinter Layout,我正在使用tkinter创建登录/注册系统。当用户单击登录或注册时,我希望所有小部件消失,新的小部件显示在屏幕上,具体取决于他们是单击登录还是注册。因此,如果他们点击登录,新的标签和文本框将出现他们的用户名和密码。问题是我使用的是.place(),我看到的教程大多使用的是pack\u-forget或grid\u-forget 我的代码: from tkinter import * class Window: def __init__(self, master):
pack\u-forget
或grid\u-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()
我的界面:
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()
无论您使用
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()
你有没有试过把它放在你忘记的地方?fhdrsdg哦,这很有效。你也可以
destroy()
这个小部件。