[Tutor] Tkinter widget (label) updated by TCP/IP message

Alan Gauld alan.gauld at yahoo.co.uk
Mon May 28 18:10:51 EDT 2018


On 28/05/18 19:56, Alejandro Chirife via Tutor wrote:
> Two questions: 
> 1. ...  How do you send an event of data arriving 
> when the events for tkinter are all about user interaction?

The easiest way is to set a global variable and use a timer
event (after()) to poll the variable periodically and update
the UI. The downside of this is that you could miss an event
if they arrive faster than the timer fires.

If you only have a single background thread running you
can just call the event handlers directly, but it gets iffy
when you have multiple threads trying to do updates at the
same time. Or if you can do manual updates via the UI - but
I don't think that's an issue here.

Most GUIs also allow you to create an event object and insert
it into the main event queue. In Tkinter its the event_generate()
command. I confess I've never had to use it but here is a
stackoverflow simplified example:

https://stackoverflow.com/questions/270648/tkinter-invoke-event-in-main-loop

This is the purest solution that lets the GUI mainloop
take care of all the sequencing conflicts implicit
in the other options.

> 2. You mentioned "    create thread to run get_network_message" 
> in your pseudocode (in main() ). > Could you guide me towards which Class to use for this?

Check out the threading module in the standard library and its
documentation. There is also a very simple example in my tutorial
in the "Concurrent Processing" topic.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list