Hi, On Thu, 22 Sep 2022 16:17:50 +0000 Massimo POZZONI via Tkinter-discuss <tkinter-discuss@python.org> wrote:
> > I'm wondering if there is any way to have a tkinter window running, > while the main program is continuing its execution. I tried with > threading but with no success. > > So far I always got the program to wait until the tkinter window is > closed. > > Is there any way to have the python main program to continue the > execution while a tkinter window is open? I am not sure what you actually want to achieve; when using threads you need to make sure that the Tcl interpreter (inside which the tkinter window is running) runs in the main program thread and that you must *never* do any calls to tkinter from within any of the child threads. For interaction between tkinter and the child threads you can for example use Lock or Condition objects to safely update some variable value from the child thread and an after() loop to query the variable value on the Tk side. Below a primitive example: ########################################## from tkinter import * from threading import Thread, Lock from time import sleep from signal import signal root = Tk() s = IntVar() s.set(0) Label(root, textvariable=s).pack(pady=30, padx=70) x = 0 run = True lock = Lock() def quit(*args): global run # make sure the child stops before we quit run = False root.quit() root.protocol('WM_DELETE_WINDOW', quit) # make sure keyboard interrupt is handled properly signal(2, quit) def poll(): lock.acquire() s.set(x) print('current value ->', x) lock.release() root.after(500, poll) poll() def incr(): global x while run: lock.acquire() x += 1 lock.release() sleep(1) print('child thread done') t = Thread(target=incr) t.start() root.mainloop() ########################################## Have a nice day, Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Without facts, the decision cannot be made logically. You must rely on your human intuition. -- Spock, "Assignment: Earth", stardate unknown _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss