I'm using python to create a class object that controls a Tkinter window. My code looks like this:
class MyGui:
def __init__(self):
self.root = tk.Tk()
self.root.title("My Gui")
return
def makeButton(self, text, on_click_function, should_pack = True):
""" Makes a button widget """
button = tk.Button(self.root, text=text, command=on_click_function)
if should_pack:
button.pack()
return button
def start(self):
self.root.mainloop()
def stop(self):
self.root.destroy()
I want to be able to start an instance of the class, then stop it like so:
a = MyGui()
a.makeButton('STOP', a.stop)
a.start()
... // OTHER CODE HERE EVENTUALLY // ...
Everything works fine and the window is successfully created on start()
and it vanishes when the button is clicked and stop()
executes. The issue is that the mainloop continues to run. I have to manually kill the program using Ctrl+C and it shows it being killed at self.tk.mainloop(n)
.
If I just do all my tkinter code in a normal file rather than a class, everything works the same but the mainloop also stops when I call destroy on root, and the program ends. If I put it in a class, however, mainloop doesn't stop in spite of the window being destroyed.
Why? How can I get it to stop mainloop too? (I can stop the whole program by calling sys.exit
, but I want to keep executing non-Gui stuff after I call stop
via the button-press).