[docs] [issue11029] Crash, 2.7.1, Tkinter and threads and line drawing
Jackmoo
report at bugs.python.org
Sun May 24 13:57:35 CEST 2015
Jackmoo added the comment:
Hi there, recently I also encounter this in windows(7), and my python program occasionally crash and the windows pops 'appcrash' message with tcl8.5.dll error c000005 (access violation).
And this end up related to thread safe problem described as above. Since in my program, I have to build the tkinter UI in another thread (main thread was occupied by other module which blocking), so I can't follow the 'build UI in main thread' solution. My work around is, add a handler which using .after() and get_nowait() to call itself periodically to handle the command in queue.
def commandQueueHandler(self):
try:
while 1:
your_command = self.textCommandQueue.get_nowait()
if your_command is not None:
# execute the command ....
self.Text.update_idletasks()
except Queue.Empty:
pass
self.Text.after(100, self.commandQueueHandler)
Once this method is triggered, just build another thread to put() command in textCommandQueue, then it will be executed periodically.
One thing that should mention is the update_idletasks() should be added after each command execution, otherwise the UI refresh will be slower then expected. (in my case, the scrolling of Text widge is a bit 'sticky' when I pull it. After adding update_idletasks(), it becomes smoother.)
TL:DR,
If you have to run tkinter UI in another thread, add a queue and self-called method with after() and get_nowait() in UI thread to handle the queue. If you want to send command to UI at other thread, just put() the command in the queue of UI thread.
----------
nosy: +Jackmoo
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11029>
_______________________________________
More information about the docs
mailing list