【发布时间】:2018-12-11 08:54:43
【问题描述】:
我正在尝试读取 GPIO 引脚作为输入,同时使用 Tkinter 作为图形模块来控制输出。到目前为止,它的证明并不好。我已经克服了绕过“while True”参数的循环问题,将其替换为 after 但睡眠问题正在锁定程序。我不太了解python,无法知道 .after 延迟的正确语法。我认为使用全局可能会有所帮助,但我不确定如何用 Python 编写它。我更像是 C、C# 和 C++ 开发人员,而且我对 Python 比较陌生。所以到目前为止我会发布我的代码,如果有人有任何想法,我肯定会在听!
import tkinter as tk
import tkinter.font
import RPi.GPIO as GPIO
from random import randint
from tkinter import Tk, Button, NSEW
from gpiozero import LED, Button
from signal import pause
win = tk.Tk()
win.title("ARBOR 4.0")
myFont = tkinter.font.Font(family = 'Helvectica', size = 24, weight = "bold")
Relay1 = LED(5) #Left Bin Selection
Relay2 = LED(6) #Right Bin Selection
Relay3 = LED(13) #Latch Release
Relay4 = LED(12) #General Bin Selection Signal
LimitSW = Button(26) #Sense Status of Door
SensePin = Button(16) #3.3V Return for System Use Status
SprinklerSense = Button(14) #Sprinkler sense
FireSense = Button(19) #Fire sense circuit
GPIO.setmode(GPIO.BCM)
# GPIO.setmode(GPIO.BOARD) Perhaps Broadcom is problem
GPIO.setup(26, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
POLLING_DELAY = 3000
def get_gpio_input():
return randint(1, 10) < 3
def check_status():
input_state1 = GPIO.input(26)
input_state2 = GPIO.input(16)
input_state3 = GPIO.input(14)
input_state4 = GPIO.input(19)
if input_state1 == True:
if input_state2 == True:
LimitSW["text"] = "System In Use"
else if input_state3 == True;
LimitSW["text"] = "System Cleaning, Please Wait!"
win.after(1)
else if input_state4 == True;
LimitSW["text"] = "Fire Alert! Please Wait!" bg='red'
else:
LimitSW["text"] = "System Available"
Relay1.off()
Relay2.off()
Relay3.off()
Relay4.off() # Reset all relays to off
def selectOption1():
if Relay1.is_lit:
Relay1.off()
paperButton["text"] = "Paper"
else:
if LimitSW["text"] == "System Available":
Relay1.on() # Signal Left Bin Selection
Relay3.on() # Open Latch
LimitSW["text"] = "System In Use"
paperButton["text"] = "Please Wait!"
win.after(1000)
Relay1.off() # Signal Left Bin Selection
Relay3.off() # Open Latch
paperButton["text"] = "Paper"
LimitSW["text"] = "System Available"
def selectOption2():
if Relay2.is_lit:
Relay1.off()
plasticButton["text"] = "Plastic"
else:
if LimitSW["text"] == "System Available":
Relay2.on() # Signal Right Bin Selection
Relay3.on() # Open Latch
LimitSW["text"] = "System In Use"
plasticButton["text"] = "Please Wait!"
win.after(1000)
Relay2.off() # Signal Left Bin Selection
Relay3.off() # Open Latch
plasticButton["text"] = "Plastic"
LimitSW["text"] = "System Available"
def selectOption3():
if Relay2.is_lit:
Relay2.off()
glassButton["text"] = "Glass/Metal"
else:
if LimitSW["text"] == "System Available":
Relay2.on() # Signal Right Bin Selection
Relay3.on() # Open Latch
LimitSW["text"] = "System In Use"
glassButton["text"] = "Pleae Wait!"
win.after(1000)
Relay2.off() # Signal Left Bin Selection
Relay3.off() # Open Latch
glassButton["text"] = "Glass/Metal"
LimitSW["text"] = "System Available"
def selectOption4():
if Relay4.is_lit:
Relay3.off()
Relay4.off()
generalButton["text"] = "General Waste"
else:
if LimitSW["text"] == "System Available":
Relay4.on() # Signal Home Bin Selection
Relay3.on() # Open Latch
LimitSW["text"] = "System In Use"
generalButton["text"] = "Pleae Wait!"
win.after(1000)
Relay4.off() # Signal Left Bin Selection
Relay3.off() # Open Latch
generalButton["text"] = "General Waste"
LimitSW["text"] = "System Available"
def exitProgram():
win.quit()
win.destroy()
LimitSW=tk.Button(win, text='System Available', font=myFont, bg='orange', height=2, width=36)
LimitSW.grid(row=0, sticky=tk.NSEW)
paperButton=tk.Button(win, text='Paper', font=myFont, command=selectOption1, bg='blue', height=2, width=36)
paperButton.grid(row=1, sticky=tk.NSEW)
plasticButton=tk.Button(win, text='Plastic', font=myFont, command=selectOption2, bg='red', height=2, width=36)
plasticButton.grid(row=2, sticky=tk.NSEW)
glassButton=tk.Button(win, text='Glass/Metal', font=myFont, command=selectOption3, bg='green', height=2, width=36)
glassButton.grid(row=3, sticky=tk.NSEW)
generalButton=tk.Button(win, text='General Waste', font=myFont, command=selectOption4, bg='purple', height=2, width=36)
generalButton.grid(row=4, sticky=tk.NSEW)
exitButton=tk.Button(win, text='Exit', font=myFont, command=exitProgram, bg='purple', height=2, width=36)
exitButton.grid(row=5, sticky=tk.NSEW)
win.after(0, check_status)
win.mainloop()
这是一个回收界面,如果有人能胜任这项任务,我真的需要帮助。这是在使用 5" 触摸屏的 Raspberry Pi 上运行的。如果有人对我有任何建议,请提前感谢!
【问题讨论】: