2016-11-05 3 views
0

Hallo alle Jungs hier, ich bin Neuling auf Python und seine Module. Also entwickle ich eine App, um das Bürolichtsystem zu überwachen. Ich verwende tkinter, um GUI zu entwickeln, ich versuche, eine Schleife innerhalb eines Buttom-Befehls zu laufen, und es friert meine GUI ein. Also lese ich etwas über das Modultreten. Ich möchte den Wert aus meiner Variablen var in einer Schleife verwenden. Ich versuche, wie Sie unten sehen können, aber die Funktion 'enable_automatico' ruft nicht meine Thread-Funktion 'acionamento_automatico' auf. Ich erhalte keine Störungen>Raspberry Pi, Tkinter und Threading

#!/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() 
+0

vielleicht verwenden 'root.after (miliseconds, function_name)' auszuführen Funktion regelmäßig und Sie werden keine Notwendigkeit, Ihre Schleife und Threads. – furas

+0

für mich 'acionamento_automatico' ist nicht Teil der' Thread' Klasse, daher kann 'enable_automatico' es nicht starten. Sie müssen den Namen 'run()' verwenden. – furas

+0

Ich werde mehr Threads in dieser App haben. Könnten Sie ein praktisches Beispiel mit Threads in meinem Code geben? Ich bin ziemlich verloren ... – armf1993

Antwort

0

Thema läuft Code, der in seiner Funktion ist run(), aber sie tun es nicht

haben
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() 

Oder man kann es kürzer

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() 
tun

EDIT: ex reichlich, wie Faden zu steuern und zu stoppen.

BTW: In einigen Situationen sollten Sie queue verwenden, um mit Thread zu kommunizieren. Nur Haupt-Thread print() verwenden sollte und arbeitet mit GUI wie 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 
+0

Furas danke. Es funktioniert gut, ich bekomme nur eine Fehlermeldung, dass mein Thread nicht in der Hauptschleife von tkinter ist. Wie kann ich mit dieser Ausnahme umgehen? – armf1993

+0

Zeigen Sie diese Ausnahme - Volltext. Der Thread muss vor 'root.mainloop()' erstellt/gestartet werden. BTW: Ich sehe 'hauptruf()' nicht in Ihrem Code - tkinter kann nicht ohne es laufen (außer wenn Sie in IDLE laufen, das 'tkinter' verwendet und' hauptroup() 'ausführt). Und Sie müssen den Thread stoppen, bevor Sie das 'tkinter'-Fenster schließen. weil thread versuchen wird, tkinter element zu verwenden, die beim Schließen des Fensters entfernt werden. – furas

+0

Ok Legend, habe es und repariere es. Vielen Dank!! – armf1993