why, when I use the destroy() method, the program does not close? I tried to use Nick.destroy(), root.destroy() and self.destroy() and none of them work. Tries to close the window when the user types in the correct nickname and passes it out of class
from tkinter import *
class Nick(Frame):
def __init__(self, master):
super(Nick, self).__init__(master)
self.master = master
self.grid()
self.mes = None
self.count = 0
self.lbl = Label(self, text="WPROWADZ NICK").grid(row=1, column=1)
self.ent =Entry(self)
self.ent.grid(row=2, column=1)
btn = Button(self, text="Akceptuj", command=self.accept)
btn.grid(row=2, column=2)
self.error = Label(self, text="")
self.error.grid(row=3, column=1)
def accept(self):
self.mes = self.ent.get()
if len(self.mes) == 0:
self.error.config(text="Nick nie moze byc pusty")
elif " " in self.mes:
self.error.config(text="Nick nie moze zawierac spacji")
else:
global mes
mes = self.mes
Nick.destroy()
if self.count == 0:
self.error.config(text="")
self.count+=1
def main_tk():
root = Tk()
root.title("ONLINE CHAT")
root.geometry("260x100")
app = Nick(root)
root.mainloop()
main_tk()
print(mes)
The quickest fix would be to replace Nick.destroy()
with self.master.destroy()
This is why the ones you've tried do not work:
Nick.destroy()
: Nick
class has no @classmethod called destroy
root.destroy()
: root
is only accessible in the main method and .mainloop
is blocking. So by the time you exit from the main loop (via closing the program, ctrl + c, etc) there's no need to destroy the root.self.destroy()
: destroying the Nick
class would only destroy the Frame
it inherits from and thus and will not close the entire program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.