I'm trying to set up a tkinter window that I can interact with outside of the main loop, using a queue. When I interpret this in spyder, it works fine. after launch()-ing, my Tk window appears, and I still have console access, allowing me to change_titre('whatever') to change the title of the window.
However, closing the window is an issue. It closes fine, checking mythread shows that the thread indeed stopped. But then calling launch() again does nothing and blocks the interpreter. I am then forced to restart python :(
Is there something that needs cleaning that prevents me from creating a new thread ? From what I'm reading around here, tkinter does not like not being run in main, which is what I'm doing here. But Why is the first instance working then ?
I would like to be able to code a few low-level functions like change_titre below (drawing basic stuff for example), and then allow the user to code his own functions using those. If all fails, is there another way to go ?
import tkinter as tk
from threading import Thread
#import threading
import queue
request_queue = None
master = None
mythread = None
def submit_to_tkinter(callable, *args, **kwargs):
request_queue.put((callable, args, kwargs))
return
def threadmain():
global master
master = tk.Tk()
master.title("stuff")
drawzone = tk.Canvas(master, width=300, height = 300, bg='white')
drawzone.pack()
def queueloop():
try:
callable, args, kwargs = request_queue.get_nowait()
except queue.Empty:
pass
else:
callable(*args, **kwargs)
master.after(500, queueloop)
queueloop()
master.mainloop()
def change_titre(text):
submit_to_tkinter(master.title,text)
return
def launch():
global mythread,request_queue
request_queue = queue.Queue()
mythread = Thread(target=threadmain, args=())
mythread.daemon=True
mythread.start()