I am looking for Tkinter mainloop() alternative that continues code after window is destroyed. Currently my code is written as follows:
root = tk.Tk()
recObj = Twiz_Soft.Recorder()
app = MyApp(root,recObj)
root.mainloop()
Contained within MyApp is a method that calls:
root = tk.Toplevel()
recObj = Twiz_Soft.Recorder(self.branch)
app = Twiz_app.BranchEditor(root,recObj)
root.mainloop()
# Code Im trying to execute once BranchEditor window is destroyed
and maintained in a separate file:
class BranchEditor(object):
""""""
def __init__(self, parent, recObj):
"""Constructor"""
self.root = parent
self.recObj = recObj
self.root.title("Twiz_BranchEditor")
self.frame = tk.Frame(parent)
self.frame.pack()
exitb = tk.Button(self.root,text='Exit',fg='red',command=self.root.destroy).pack()
Reference to the 'Twiz' package is a custom block of code I'm writing.
I have found solution to my problem that utilizes:
var = tk.IntVar()
button = tk.Button(inputFrame, text="exit",fg='green', command=lambda: var.set(1))
button.pack()
print("waiting...")
button.wait_variable(var)
print("done waiting.")
However I was hoping for something a little more elegant since triggering button.wait_variable(var)
within an init function seems pretty clumsy and goes against my typical expectation of syntax.
Thank you so much in advance!
You shouldn’t call mainloop
more than once. You don’t need to call it for each Toplevel
. Any code after the one call will run once the window has been destroyed.
If you need to wait until the Toplevel
has been destroyed, you can call its wait_window
method.