使用Tkinter按钮时Python读取GPIO输入引脚时出现问题

时间:2018-12-11 08:54:43

标签: python button tkinter

我正在尝试将GPIO引脚读取为输入,同时使用Tkinter作为图形模块来控制输出。到目前为止,还不能很好地证明这一点。我克服了循环问题,解决了“ while True”自变量替换为after但sleep问题锁定程序的问题。我对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上运行。如果有人对我有任何建议,请先感谢!

1 个答案:

答案 0 :(得分:0)

您可以在无限while循环中执行一个线程,该线程检查引脚的状态(ON / OFF)并相应地执行操作。 您也可以设置一个中断,但是这会有些复杂,并且可能会导致中断。您可以参考以下代码并在此处和此处进行调整以满足您的需求:

import RPi.GPIO as GPIO
import time
import tkinter as tk



class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()
        self.pi_button()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = lambda: self.say_hi('tk button pressed')
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=root.destroy)
        self.quit.pack(side="bottom")


    def pi_button(self):
        #Setup GPIO for input
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        self.input_state = GPIO.input(18)
        #Check to see if button is pressed, if it is print notification
        if self.input_state == False:
            self.say_hi('rpi button pressed')
        #Use tks main loop as a while loop to call for a check every .5 seconds
        self.after(500, self.pi_button)

    def say_hi(self, text):

        print(text)



root = tk.Tk()
app = Application(master=root)
app.mainloop()