tkinter 如何在不单击按钮的情况下获取标签以显示 GPIO 状态

tkinter how to get a label to refresh without a button click to show GPIO state

此代码是我正在开发的水族馆控制器图形用户界面的一部分。当它启动时,它会自动在标签****状态标签中显示 GPIO 引脚的当前状态。我想做的是设法让它每 20 秒刷新一次标签。如果这可以通过单击按钮来完成,我更愿意..我不确定这是否可能或者 not.I 是 python 的新手所以任何帮助将不胜感激

import RPi.GPIO as GPIO
import time
import tkinter as tk
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT)   #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT)   #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT)   #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT)   #used for return pump 1
GPIO.setup(9, GPIO.OUT)   #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT)  #power head 1 ch
GPIO.setup(19, GPIO.OUT)  #SPARE relay output
GPIO.setup(14, GPIO.OUT)  #SPARE relay output
GPIO.setup(15, GPIO.OUT)  #SPARE relay output
GPIO.setup(18, GPIO.OUT)  #SPARE relay outlet
#set the outputs to high on startup
#GPIO.output(2, GPIO.HIGH)  # return pump 1
#GPIO.output(3, GPIO.HIGH)  # return pump 2
#GPIO.output(4, GPIO.HIGH)  # gyre pump
#GPIO.output(9, GPIO.HIGH)  # powerhead 2
#GPIO.output(11, GPIO.HIGH) # powerhead 1
#GPIO.output(19, GPIO.HIGH) # Spare
#GPIO.output(14, GPIO.HIGH) # Spare
#GPIO.output(15, GPIO.HIGH) # Spare
#GPIO.output(18, GPIO.HIGH) # Spare
time.sleep(1)
#setup tkinter
root=tk.Tk()
root.title("Johns Aquarium")
root.geometry("800x550")
root.configure(bg="lightblue")
#setup image 
photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
label1 = tk.Label(image=photo1)#puts label in the window in this case not text file must be in program folder
label1.grid(row=10, column=0, columnspan=12) #says how to place the label
#setup fonts
ftlab= 'Verdana', 13, 'bold'
ftb= 'Verdana', 11, 'bold'
#setup exit button
Exitbutton= tk.Button(root, text="Exit", font=(ftb), width=6, bg="red", fg="white", command=root.destroy)
Exitbutton.place(x=700, y=240)
#setup return pump 1
labelreturn1= tk.Label(root, text=("RETURN PUMP 1"), font=(ftlab), bg="yellow", fg="black")
labelreturn1.place(x=0, y=300)
labelreturn1gpio= tk.Label(root, text=("GPIO          2"), font=(ftlab), bg="black", fg="white")
labelreturn1gpio.place(x= 670, y=300)
labelreturn1state= tk.Label(root, font=(ftlab), fg="black")
labelreturn1state.place(x=550, y=296)
labelreturn1state.configure(text='   Auto   ' if GPIO.input(2) else '     Off   ')
#setup return pump 2
labelreturn2= tk.Label(root, text=("RETURN PUMP 2"), font=(ftlab), bg="yellow", fg="black")
labelreturn2.place(x=0, y=335)
labelreturn2gpio= tk.Label(root, text=("GPIO          3"), font=(ftlab), bg="black", fg="white")
labelreturn2gpio.place(x= 670, y=335)
labelreturn2state= tk.Label(root, font=(ftlab), fg="black")
labelreturn2state.place(x=550, y=331)
labelreturn2state.configure(text='     On    ' if GPIO.input(3) else '     Off   ')

#setup gyre pump buttons and labels
labelgyre= tk.Label(root, text=("GYRE WAVE       "), font=(ftlab), bg="green", fg="black")
labelgyre.place(x=0, y=400)
labelgyregpio= tk.Label(root, text=("GPIO         4"), font=(ftlab), bg="black", fg="white")
labelgyregpio.place(x= 670, y=400)
labelgyrestate= tk.Label(root, font=(ftlab), fg="black")
labelgyrestate.place(x=550, y=396)
labelgyrestate.configure(text='     On    ' if GPIO.input(4) else '     Off   ')
#setup power head 1 pump
labelpwrhd1= tk.Label(root, text=("POWER HEAD  1"), font=(ftlab), bg="orange", fg="black")
labelpwrhd1.place(x=0, y=450)
labelpwrhd1gpio= tk.Label(root, text=("GPIO        11"), font=(ftlab), bg="black", fg="white")
labelpwrhd1gpio.place(x= 670, y=450)
labelpwrhd1state= tk.Label(root, font=(ftlab), fg="black")
labelpwrhd1state.place(x=550, y=446)
labelpwrhd1state.configure(text='     On    ' if GPIO.input(11) else '     Off   ')
#setup powerhead 2 buttons and labels
labelpwrhd2= tk.Label(root, text=("POWER HEAD  2"), font=(ftlab), bg="orange", fg="black")
labelpwrhd2.place(x=0, y=496)
labelpwrhd2gpio= tk.Label(root, text=("GPIO          9"), font=(ftlab), bg="black", fg="white")
labelpwrhd2gpio.place(x= 670, y=496)
labelpwrhd2state= tk.Label(root, font=(ftlab), fg="black")
labelpwrhd2state.place(x=550, y=496)
labelpwrhd2state.configure(text='     On    ' if GPIO.input(9) else '     Off   ')

root.mainloop()

您可以通过使用 Tk 对象的 after 方法定期 运行 函数。

def update():
    on, off = '     On    ', '     Off   '
    labelreturn2state['text'] = on if GPIO.input(3) else off
    # et cetera
    # Re-register the timeout.
    root.after(20000, update)


# Register the timeout. Time is in ms.
root.after(20000, update)
# Start the main loop.
root.mainloop()

好吧,在尝试之后,我设法通过以下代码找到了解决此问题的答案。我看到的唯一问题是,当我单击提要按钮时,程序要求 GPIO 引脚停止 20 分钟。所以它不会在这个休息期间更新标签。但希望这可以帮助人们完成它的方法

import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT)   #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT)   #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT)   #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT)   #used for return pump 1
GPIO.setup(9, GPIO.OUT)   #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT)  #power head 1 ch 
ftlab= 'Verdana', 13, 'bold'
ftb= 'Verdana', 11, 'bold'
#setup tkinter
def rtn1auto():
    GPIO.output(2, GPIO.HIGH)
def rtn1off():
    GPIO.output(2, GPIO.LOW)
def rtn2on():
    GPIO.output(3, GPIO.HIGH)
def rtn2off():
    GPIO.output(3, GPIO.LOW)
def pwrhd1on():
    GPIO.output(11, GPIO.HIGH)
def pwrhd1off():
    GPIO.output(11, GPIO.LOW)
def pwrhd2on():
    GPIO.output(9, GPIO.HIGH)
def pwrhd2off():
    GPIO.output(9, GPIO.LOW)
def gyreon():
    GPIO.output(4, GPIO.HIGH)
def gyreoff():
    GPIO.output(4, GPIO.LOW)
def rtn1feed():
    time.sleep(15)
    GPIO.output(2, GPIO.LOW)
    time.sleep(3)
    GPIO.output(3, GPIO.LOW)
    time.sleep(3)
    GPIO.output(4, GPIO.LOW)
    time.sleep(3)
    GPIO.output(11, GPIO.LOW)
    time.sleep(3)
    GPIO.output(9, GPIO.LOW)
    time.sleep(1200)
    GPIO.output(2, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(3, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(4, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(11, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(9, GPIO.HIGH)
class gpio(tk.Tk):
    def __init__(root, *args, **kwargs):
        tk.Tk.__init__(root, *args, **kwargs)
        root.title("johns")
        root.geometry("800x550")
        root.configure(bg="lightblue")
        #setup exit button
        Exitbutton= tk.Button(root, text="Exit", font=(ftb), width=6, bg="red", fg="white", command=root.destroy)
        Exitbutton.place(x=700, y=240)
        #setup image 
        #photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
        #label1 = tk.Label(root, image=photo1)#puts label in the window in this case not text file must be in program folder
        #label1.place(x=0, y=0) #says how to place the label

    #set up return pump 1 buttons and labels
        root.rtnp1label = tk.Label(root, text="")
        root.rtnp1label.place(x=560, y=300)
        labelreturn1= tk.Label(root, text=("RETURN PUMP 1"), font=(ftlab), bg="yellow", fg="black")
        labelreturn1.place(x=0, y=300)
        butreturn1auto= tk.Button(root, text=("AUTO RUN"), font=(ftb), command=rtn1auto)
        butreturn1auto.place(x=180, y=296)
        butreturn1off= tk.Button(root, text=("PUMP OFF"), font=(ftb), command=rtn1off)
        butreturn1off.place(x= 300, y=296)
        labelreturn1gpio= tk.Label(root, text=("GPIO          2"), font=(ftlab), bg="black", fg="white")
        labelreturn1gpio.place(x= 670, y=300)
    #setup return pump 2 buttons and labels                  
        root.rtnp2label = tk.Label(root, text="")
        root.rtnp2label.place(x=560, y=335)
        labelreturn2= tk.Label(root, text=("RETURN PUMP 2"), font=(ftlab), bg="yellow", fg="black")
        labelreturn2.place(x=0, y=335)
        butreturn2on= tk.Button(root, text=("PUMP  ON"), font=(ftb), command=rtn2on)
        butreturn2on.place(x=180, y=331)
        butreturn2off= tk.Button(root, text=("PUMP OFF"), font=(ftb), command=rtn2off)
        butreturn2off.place(x= 300, y= 331)
        labelreturn2gpio= tk.Label(root, text=("GPIO          3"), font=(ftlab), bg="black", fg="white")
        labelreturn2gpio.place(x= 670, y=335)
    #setup powerhead 2 buttons and labels
        root.pwrhd2label = tk.Label(root, text="")
        root.pwrhd2label.place(x=560, y=496)
        labelpwrhd2= tk.Label(root, text=("POWER HEAD  2"), font=(ftlab), bg="orange", fg="black")
        labelpwrhd2.place(x=0, y=496)
        butpwrhd2= tk.Button(root, text=("PUMP  ON"), font=(ftb), command=pwrhd2on)
        butpwrhd2.place(x=180, y=492)
        butpwrhd2= tk.Button(root, text=("PUMP OFF"), font=(ftb), command=pwrhd2off)
        butpwrhd2.place(x= 300, y= 492)
        labelpwrhd2gpio= tk.Label(root, text=("GPIO          9"), font=(ftlab), bg="black", fg="white")
        labelpwrhd2gpio.place(x= 670, y=496)
    #setup powerhead 1 buttons and lbels
        root.pwrhd1label = tk.Label(root, text="")
        root.pwrhd1label.place(x=560, y=450)
        labelpwrhd1= tk.Label(root, text=("POWER HEAD  1"), font=(ftlab), bg="orange", fg="black")
        labelpwrhd1.place(x=0, y=450)
        butpwrhd1= tk.Button(root, text=("PUMP  ON"), font=(ftb), command=pwrhd1on)
        butpwrhd1.place(x=180, y=446)
        butpwrhd1= tk.Button(root, text=("PUMP OFF"), font=(ftb), command=pwrhd1off)
        butpwrhd1.place(x= 300, y= 446)
        labelpwrhd1gpio= tk.Label(root, text=("GPIO        11"), font=(ftlab), bg="black", fg="white")
        labelpwrhd1gpio.place(x= 670, y=450)
    #setup gyre pump buttons and labels
        root.gyrelabel =tk.Label(root, text="")
        root.gyrelabel.place(x=560, y=400)
        labelgyre= tk.Label(root, text=("GYRE WAVE       "), font=(ftlab), bg="green", fg="black")
        labelgyre.place(x=0, y=400)
        butgyreon= tk.Button(root, text=("PUMP  ON"), font=(ftb), command=gyreon)
        butgyreon.place(x=180, y=396)
        butgyreoff= tk.Button(root, text=("PUMP OFF"), font=(ftb), command=gyreoff)
        butgyreoff.place(x= 300, y= 396)
        labelgyregpio= tk.Label(root, text=("GPIO         4"), font=(ftlab), bg="black", fg="white")
        labelgyregpio.place(x= 670, y=400)
    #setup feed button       
        butreturn1feed= tk.Button(root, text=("20 MINUTE ") + ("FEED TIMER"),wraplength=90, font=(ftb),bg="red",
                                  height=12, command=rtn1feed)
        butreturn1feed.place(x= 420, y=296) 
        # start the GPIO checks"
        root.update_gpio()

    def update_gpio(root):
        rt1 = ('      On      ' if GPIO.input(2) else '      Off     ')
        root.rtnp1label.configure(text=rt1, bg ="yellow", font=(ftb))
        rt2 = ('      On      ' if GPIO.input(3) else '      Off     ')
        root.rtnp2label.configure(text=rt2, bg ="yellow", font=(ftb))
        ph2 = ('      On      ' if GPIO.input(9) else '      Off     ')
        root.pwrhd2label.configure(text=ph2, bg ="yellow", font=(ftb))
        ph1 = ('      On      ' if GPIO.input(11) else '      Off     ')
        root.pwrhd1label.configure(text=ph1, bg ="yellow", font=(ftb))
        gyr = ('      On      ' if GPIO.input(4) else '      Off     ')
        root.gyrelabel.configure(text=gyr, bg ="yellow", font=(ftb))
        # call this function again in one second
        root.after(200, root.update_gpio)



if __name__== "__main__":
    app = gpio()
    app.mainloop()