首页 > 解决方案 > tkinter 如何在不单击按钮的情况下刷新标签以显示 GPIO 状态

问题描述

这段代码是我正在处理的水族馆控制器 gui 的一部分。当它启动时,它会自动在 label****state LABEL 中显示 GPIO 引脚的当前状态。我想做的是让它每 20 秒刷新一次标签。我希望这可以通过单击按钮来完成。我不确定这是否可能。我是 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()

标签: pythontkintergpio

解决方案


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

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

推荐阅读