python 3.x - tkinter sequential execution ordering -
i having trouble code won't execute code @ point. can't describe problem in wording, in code, remaining code under window.mainloop()
not run need unless main window (gui) closed problematic. want able print(t.test)
when code under def calculate has been executed. technically removing window.mainloop()
run code before can gather input vital. need keep structure of coding same shown below well.
import tkinter class test: def __init__(self): self.test = false def calculate(window, userinput, t): test = userinput.get() print(test) window.destroy() t.test = true def main(): t = test() gui = tkinter.tk() gui.title("example window") gui.geometry("400x400") userinput = tkinter.stringvar() window = tkinter.toplevel() window.title("entry") tkinter.message(window, text="label", width="200").pack() tkinter.entry(window, width=30, textvariable=userinput).pack() tkinter.button(window, text="ok", command=lambda: calculate(window, userinput, t)).pack() window.mainloop() print(t.test) gui.mainloop() main()
edit: seems able make behave same way , keep same structure putting print(t.test)
under own defined function , calling under def calculate(userinput, window, t)
in last line. tested causes main window freeze.
the way tkinter designed work you:
- create widgets
- call mainloop (exactly once)
- the window destroyed , program exits
you shouldn't call mainloop more once, , shouldn't have more code after call mainloop.
if want run after call mainloop, schedule after
:
... gui.after(1, do_something) gui.mainloop()
anything scheduled after
run after mainloop has started.
Comments
Post a Comment