Re: [Tutor] get key value from Redis instance

2018-05-28 Thread Silviu Chiric
Thank you Danny,

indentation and separation of code  worked out as you mentioned :)

Have a nice day!

On Mon, May 28, 2018 at 12:14 AM, Danny Yoo  wrote:

> >   From a first glance: it looks like one of your print statements is not
> > vertically aligned with the rest of your print statements, so I would
> > expect Python to be reporting a SyntaxError because of this misalignment.
>
> Substitute the word "vertical" with "horizontal".   Sorry: I made yet
> another mental typo there.  :P
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-05-28 Thread Alan Gauld via Tutor
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-05-28 Thread Mats Wichmann
On 05/28/2018 12:56 PM, Alejandro Chirife via Tutor wrote:
>  Hi Alan,
> Thank you very much for your help.   I will start working on ti.  There's a 
> lot to chew on here :)
> Two questions: 
> 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?
> Thanks!AC

You're going to want to look at the threading module for threads, and
the socket and the select modules for networking. threading.Event and
threading.Timer may be of interest as to communicating with the other
thread and setting a timeout.

This isn't actually as trivial a problem as it may sound on the surface,
by the way, so don't worry if it takes a while to get right!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-05-28 Thread Alejandro Chirife via Tutor
 Hi Alan,
Thank you very much for your help.   I will start working on ti.  There's a lot 
to chew on here :)
Two questions: 
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?
Thanks!AC




On Sunday, May 27, 2018, 1:04:22 PM EDT, Alan Gauld via Tutor 
 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
reasons!).

> 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
the label)

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

def button_click()
    cancel timer
    display default message

def display_message(msg):
    display msg on label
    cancel current timer
    create new 10s timer

def timeout():
    display ???

def get_network_message()
    your network receiving code here
    
    if is_valid_message(msg):
      display_message(msg)

def main():
    create GUI
    bind events
    create thread to run get_network_message
    mainloop()


HTH
-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor