tkinter

Peter Otten __peter__ at web.de
Tue Mar 19 05:06:27 EDT 2019


Terry Reedy wrote:

> On 3/18/2019 12:00 PM, Informatico de Neurodesarrollo wrote:
>> Hello friends:
>> 
>> I am a beginner on programming in python.
>> 
>> I want make a simple program that test continuously (every 5 seg) the
>> connection  to internet and change the background color when are not
>> available. I try this , but not work properly:
>> 
>>  #!/usr/bin/env python3
>> # -*- coding: utf-8 -*-
> 
> Not needed for py 3 where utf-8 is default
> 
>> from tkinter import *
>> import socket, time
>> 
>> def DetectarConexion():
>>  testConn = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>  try:
>>  testConn.connect(('8.8.8.8', 80))
>>  testConn.close()
>>  return True
>>  except:
>>  testConn.close()
>>  return False
>> 
>> root = Tk()
>> root.title("Conexión")
>> root.geometry("80x50")
>> 
>> while True:
>>  if DetectarConexion():
>>  # Background:Green
>>  root.config(background="#38EB5C")
>>  else:
>>  # Background:Red
>>  root.config(background="#F50743")
>>  time.sleep(5)
> 
> This is your problem.  Loop with event loop in order to not block gui.
> See working code below.
> 
>> root.mainloop()
> 
> #!/usr/bin/env python3
> 
> from tkinter import *
> 
> connected = False
> def DetectarConexion():
>      global connected
>      connected = not connected
>      return connected
> 
> root = Tk()
> root.title("Conexión")
> root.geometry("80x50")
> 
> def colorupdate():
>      if DetectarConexion():
>          # Background:Green
>          root.config(background="#38EB5C")
>      else:
>          # Background:Red
>          root.config(background="#F50743")
>      root.after(1000, colorupdate)  # 1000 milliseconds = 1 second
> 
> colorupdate()
> root.mainloop()

If the DetectarConexion() function takes long to execute it will still block 
the GUI because it is called indirectly by the callback. 

In that case you may execute it in a separate thread:

#!/usr/bin/env python3

from tkinter import *

import random
import threading
import time

# global flag used to communicate between the checker thread
# and the GUI in the main thread. If the interaction gets more
# complex you may use a queue.Queue() instead.
connected = False


def DetectarConexion():
    # simulate that the function takes
    # 3 seconds to execute
    time.sleep(3)
    return random.random() < .7  # good with 70% probability


def update_connection_state():
    global connected
    while True:
        connected = DetectarConexion()
        time.sleep(1)  # wait 1 second until the next check


def colorupdate():
    if connected:
        # Background:Green
        root.config(background="#38EB5C")
    else:
        # Background:Red
        root.config(background="#F50743")
    root.after(1000, colorupdate)  # 1000 milliseconds = 1 second


checker_thread = threading.Thread(
    target=update_connection_state, daemon=True
)
checker_thread.start()

root = Tk()
root.title("Conexión")
root.geometry("80x50")

colorupdate()
root.mainloop()





More information about the Python-list mailing list