Sunday, November 14, 2021

[SOLVED] Raspberry pi, tkinter and threading

Issue

I am developing an app to monitor the office light system. I am using Tkinter to develop GUI, I try to run a loop within a button command and it freezes my GUI. So I read a little about the treading module. I want to use the value returning from my variable var in a loop. I try as you can see below, but the function enable_automatico doesn't call my thread function acionamento_automatico. I don't get any error>

#!/usr/bin/python3
# Importando os pacotes
import sys
import time
import threading
import RPi.GPIO as GPIO
from tkinter import ttk
from tkinter import*
import tkinter as tk
from tkinter import messagebox


#Configurando a I/O
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#Configurando os pinos de Entrada
GPIO.setup(18, GPIO.IN) #Sensor de Presença Vendas
GPIO.setup(23, GPIO.IN) #Sensor de Luminosidade Vendas
GPIO.setup(24, GPIO.IN) #Sensor de Presença Engenharia
GPIO.setup(25, GPIO.IN) #Sensor de Luminosidade Engenharia
GPIO.setup(12, GPIO.IN) #Sensor de Presença Compras
GPIO.setup(16, GPIO.IN) #Sensor de Luminosidade Compras
GPIO.setup(20, GPIO.IN) #Sensor de Presença Marketing
GPIO.setup(21, GPIO.IN) #Sensor de Luminosidade Marketing


#Configurando os pinos de Saída
GPIO.setup(17, GPIO.OUT)#Luminária A Vendas
GPIO.setup(27, GPIO.OUT)#Luminária B Vendas
GPIO.setup(22, GPIO.OUT)#Luminária A Engenharia
GPIO.setup(5, GPIO.OUT) #Luminária B Engenharia
GPIO.setup(6, GPIO.OUT) #Luminária A Compras
GPIO.setup(13, GPIO.OUT)#Luminária B Compras
GPIO.setup(19, GPIO.OUT)#Luminária A Marketing
GPIO.setup(26, GPIO.OUT)#Luminária B Marketing



#Configurando os pinos de saída  para o modo de operação manual
GPIO.output(17, GPIO.LOW)#Luminária A Vendas
GPIO.output(27, GPIO.LOW)#Luminária B Vendas
GPIO.output(22, GPIO.LOW)#Luminária A Engenharia
GPIO.output(5, GPIO.LOW) #Luminária B Engenharia
GPIO.output(6, GPIO.LOW) #Luminária A Compras
GPIO.output(13, GPIO.LOW)#Luminária B Compras
GPIO.output(19, GPIO.LOW)#Luminária A Marketing
GPIO.output(26, GPIO.LOW)#Luminária B Marketing


#Definição da fonte de texto
LARGE_FONT = ("Verdana", 22)
MEDIUM_FONT = ("Verdana", 16)
SMALL_FONT = ("Verdana", 12)



#A class thread

class MyThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        selfname = name
        self.counter=counter


def acionamento_automatico(thread1, counter):

    while (app.frames[Acionamento].var.get()==2):

        if ((GPIO.input(18)==False) and (GPIO.input(23)==False)):
            GPIO.output(17, GPIO.HIGH)
            GPIO.output(27, GPIO.LOW)
            app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.label6.configure(text="Ligado")
            app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.label7.configure(text="Desligado")

        if ((GPIO.input(18)==True) and (GPIO.input(23)==False)):
            GPIO.output(17, GPIO.LOW)
            GPIO.output(27, GPIO.LOW)
            app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.label6["text"]="Desligado"
            app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.label7["text"]="Desligado"

        if ((GPIO.input(18)==False) and (GPIO.input(23)==True)):
            GPIO.output(17, GPIO.HIGH)
            GPIO.output(27, GPIO.HIGH)
            app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.label6["text"]="Ligado"
            app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.label7["text"]="Ligado"
        

        if ((GPIO.input(18)==True) and (GPIO.input(23)==False)):
            GPIO.output(17, GPIO.LOW)
            GPIO.output(27, GPIO.LOW)
            app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.label6["text"]="Desligado"
            app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.label7["text"]="Desligado"
        
    thread1.exit()

#Habilitar modo automático
def enable_automatico():

    automatico_message = messagebox.showinfo(title="Modo Automático", message = "O acionamento das luminárias será feito conforme luminosidade e pessoas no setor, resultando num menor consumo de energia")

    app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.lev_button.configure(state=DISABLED)
    app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.ldv_button.configure(state=DISABLED)
    app.frames[Acionamento].engenhariaFrame.luminaria_esquerdaFramee.lee_button.configure(state=DISABLED)
    app.frames[Acionamento].engenhariaFrame.luminaria_direitaFramee.lde_button.configure(state=DISABLED)
    app.frames[Acionamento].comprasFrame.luminaria_esquerdaFramec.lec_button.configure(state=DISABLED)
    app.frames[Acionamento].comprasFrame.luminaria_direitaFramec.ldc_button.configure(state=DISABLED)
    app.frames[Acionamento].marketingFrame.luminaria_esquerdaFramem.lem_button.configure(state=DISABLED)
    app.frames[Acionamento].marketingFrame.luminaria_direitaFramem.ldm_button.configure(state=DISABLED)
    app.frames[Acionamento].vendasFrame.luminaria_esquerdaFramev.lev_button.update()
    app.frames[Acionamento].vendasFrame.luminaria_direitaFramev.ldv_button.update()
    app.frames[Acionamento].engenhariaFrame.luminaria_esquerdaFramee.lee_button.update()
    app.frames[Acionamento].engenhariaFrame.luminaria_direitaFramee.lde_button.update()
    app.frames[Acionamento].comprasFrame.luminaria_esquerdaFramec.lec_button.update()
    app.frames[Acionamento].comprasFrame.luminaria_direitaFramec.ldc_button.update()
    app.frames[Acionamento].marketingFrame.luminaria_esquerdaFramem.lem_button.update()
    app.frames[Acionamento].marketingFrame.luminaria_direitaFramem.ldm_button.update()

    global thread1
    thread1 = MyThread(1, "Thread-1", 1)
    thread1.start()

Solution

Thread runs code which is in its function run() but you don't have it

from threading import Thread
import time

# -----

def my_function(name, counter):
    for x in range(counter):
        print(name, x)
        time.sleep(0.5)

# -----

class MyThread(Thread):

    def run(self):
        my_function("Hello", 10)

# -----

thread = MyThread()
thread.start()

Or you can do it shorter

from threading import Thread
import time

# -----

def my_function(name, counter):
    for x in range(counter):
        print(name, x)
        time.sleep(0.5)

# -----

thread = Thread( target=my_function, args=("World", 10) )
thread.start()

EDIT: example how to control thread and stop it.

BTW: In some situations you should use queue to communicate with thread. Only main thread should use print() and works with GUI like tkinter

from threading import Thread
import time

# -----

def my_function(name, counter):
    for x in range(counter):
        if stop:
            print('STOP')
            break
        print(name, x)
        time.sleep(0.5)

# -----

stop = False

thread = Thread( target=my_function, args=("World", 20) )

print("alive 0:", thread.is_alive()) # False

thread.start()

print("alive 1:", thread.is_alive()) # True

time.sleep(2)

print("alive 2:", thread.is_alive()) # True

time.sleep(2)

print("alive 3:", thread.is_alive()) # True
stop = True
print("alive 4:", thread.is_alive()) # True

time.sleep(2)

print("alive 5:", thread.is_alive()) # False

# wait till thread ends
thread.join()

print("alive 5:", thread.is_alive()) # False


Answered By - furas