[CentralOH] How-To non-blocking GUI with Tkinter

Mark E Erbaugh mark at microenh.com
Thu Mar 1 14:18:30 CET 2012


On Feb 29, 2012, at 10:38 PM, pybokeh wrote:

> Hello,
> I am new to GUI programming and threading in Tkinter and I am basically trying to create a chat or instant messaging program for fun or as a learning experience.  The problem I am having is my while loop doesn't loop through due to the mainloop() function since it doesn't return to the main thread until the root is closed or exited.  So what ends up happening is I can receive an instant message only once from the client, but server can send message ok and the client receives the message fine.  I've been doing a lot of searching and it appears I need to either run a separate thread for the part of the program that is waiting for the data from the client.  I've also looked at tkinter.createfilehandler(), but I just don't know how to implement this.  Any help with this in terms of documentation or reference material would be greatly appreciated.  Thanks in advance!
> 
> - Daniel

Daniel,

If your checkdata() function blocks, you should run it in a separate thread.  It it doesn't block, you can call checkdata() within the main loop, by calling widget methods after() or after_idle().  after() will call its callback (checkdata) after approximately the specified number of seconds. after_idle() will call it's callback when the event loop is empty.  Either method only calls its callback once, so the callback method needs to reload itself with another call to after() or after_idle().

Mark


More information about the CentralOH mailing list