我是一名python初学者。尝试制作一个新按钮来关闭窗口。我得到的错误信息:TclError:无法调用“销毁”命令:应用程序已被销毁
Exception in Tkinter callback Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in call return self.func(*args) File "tk_cp_successful.py", line 138, in buttonPushed self.root.destroy() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1859, in destroy self.tk.call('destroy', self._w) TclError: can't invoke "destroy" command: application has been destroyed
class LoginPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
self.root = tk.Tk()
global entry_1
global entry_2
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Welcome to VISA Login Page",fg="blue")
label.pack(pady=10,padx=10)
label_1 = Label(self, text="Username")
label_1.pack()
label_2 = Label(self, text="Password")
label_2.pack()
entry_1 = Entry(self)
entry_1.pack()
entry_2 = Entry(self, show="*")
entry_2.pack()
label_1.grid(row=0, sticky=E)
label_1.pack()
label_2.grid(row=1, sticky=E)
label_2.pack()
entry_1.grid(row=0, column=1)
entry_1.pack()
entry_2.grid(row=1, column=1)
entry_2.pack()
checkbox = Checkbutton(self, text="Keep me logged in")
checkbox.grid(columnspan=2)
checkbox.pack()
logbtn = Button(self, text="Login", command = self._login_btn_clickked)
logbtn.grid(columnspan=2)
logbtn.pack()
myButton = Button(self, text="Exit",command = self.buttonPushed)
myButton.pack()
def buttonPushed(self):
self.root.destroy()
def _login_btn_clickked(self):
#print("Clicked")
username = entry_1.get()
password = entry_2.get()
#print(username, password)
if username == "test" and password == "test":
#box.showinfo("Login info", "Welcome Tester")
button1 = ttk.Button(self, text="Please click, Welcome to login!!!",
command=lambda: self.controller.show_frame(StartPage))
button1.pack()
else:
box.showerror("Login failed", "Incorrect username")
我从来没有见过这样的:'logbtn.grid(columnspan = 2); logbtn.pack()',也就是“夹持”一个小部件并在“打包”之后立即执行。我不知道什么是真正的效果,但我几乎可以肯定,这不是“正确”的做法。 – nbro
感谢您的帮助。我可以在不破坏的情况下运行它。一切正常。一旦我运行buttonPressed,它会得到错误。感觉根是全球性的,它不能从这个类中使用--------。self.root.destroy() – jack