[Tutor] Tkinter widget (label) updated by TCP/IP message
acb001 at yahoo.com
Mon May 28 14:56:07 EDT 2018
Thank you very much for your help. I will start working on ti. There's a lot to chew on here :)
1. Got it that nothing to do when mainloop() gets called. How do you send an event of data arriving when the events for tkinter are all about user interaction?
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?
On Sunday, May 27, 2018, 1:04:22 PM EDT, Alan Gauld via Tutor <tutor at python.org> wrote:
On 27/05/18 16:18, Alejandro Chirife via Tutor wrote:
> Hi all, I am having a hard time to create what at first looked like a simple program with Python 3 and Tkinter:
> The UI consist of a window with a label and a button.
> The label shows "waiting for a message" and the button shows "reset display".
> The handler for the button click just resets the label text to "waiting for a message".
So far so good, it is all standard Tkinter programming but....
> The program would draw the window with the two widgets with root.mainloop()> while it is capable of listening on a TCP port for an arriving message
This is not so good. You can't do anything in your code after you call
mainloop(). Thats just the nature of event driven programming. Once
mainloop() is called all control rests with the GUI and you can only
respond to events. Sooo....
> like "hello world", and when it arrives it would show the text in the label.
You need to put your network listener in a separate thread started
before you call mainloop(). Then when a message is received you need to
generate an event in your Tkinter GUI (or you could just call an event
handler directly but that's considered bad practice (for some good
> The received message should show in the label until any of the following occurs:
> - Has been shown for 10 seconds
Set a timer in the GUI that expires after 10 seconds
- Another packet arrived with new message (will show the new message in
Generate (or call) the same event as before but don't
forget to cancel the current timer.
- User clicks on the "reset display" button
In the button event handler
- Cancel the timer.
- Display whatever the reset message says
So in pseudo code you need:
import tkinter as tk
display default message
display msg on label
cancel current timer
create new 10s timer
your network receiving code here
create thread to run get_network_message
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
More information about the Tutor