python - Destroy everything inside a window outside of function Tkinter -
in code below destroy inside window root when gamebutton button pressed, other things happen way button run function. when perform self.destroy outside of main class, nothing deleted, there way around this?
from pil import image, imagetk tkinter import tk, label, both,w, n, e, s, entry, text, insert, toplevel ttk import frame, style, button, label import tkinter import callingwordlist difficulty = "" class mainmenuui(frame): def __init__(self, parent): frame.__init__(self, parent) self.parent = parent self.initui() def initui(self): self.parent.title("type!") self.pack(fill=both, expand=1) style = style() style.configure("tframe", background="black") logo = image.open("type!.png") typelogo = imagetk.photoimage(logo) label1 = label(self, image=typelogo) label1.image = typelogo label1.place(x=342,y=80) label1.pack() self.pack(fill=both, expand=1) gamebutton = button(self, text="main game", command=lambda: main2(self.parent,self)) gamebutton.pack() gamebutton.place(x=344,y=200,height = 80,width = 176) tutorialbutton = button(self,text="tutorial level") tutorialbutton.pack() tutorialbutton.place(x=344, y=300 ,height = 80,width = 176) quitbutton = button(self, text= "quit",command=self.parent.destroy) quitbutton.place(x=344, y=400,height = 80,width = 176) def main2(root,self): self.destroy app = maingameui(root) root.mainloop() class maingameui(root): .... def main(): root = tk() root.geometry("860x640+300+300") app = mainmenuui(root) root.mainloop() if __name__ == '__main__': main()
you aren't destroying in function. @ code:
def main2(root,self): self.destroy app = maingameui(root) root.mainloop()
notice first line in function trying destroy everything. code self.destroy
- notice lack of parenthesis. aren't calling function, referencing it. add parentheses call it: self.destroy()
.
you have problem in you're calling function destroys widget calls function. however, function enters endless loop (mainloop()
), button command never return. i'm not entirely sure happen here, you'll sort of error. bottom line is, calling mainloop
button command not idea.
since structuring app such app frame (rather root window) don't need restart event loop. when destroy mainmenuui
widget, event loop continue run. there's no need restart it.
Comments
Post a Comment