這可以在啟動(dòng)時(shí)顯示標(biāo)簽,它顯示 gpio 9 的原始狀態(tài),但是當(dāng)您單擊按鈕時(shí),它不會(huì)更改標(biāo)簽文本。我希望它讀取 gpio 的實(shí)際狀態(tài),而不是單擊按鈕的事實(shí)。任何幫助,將不勝感激。#tktrial#!/usr/bin/env python3import RPi.GPIO as GPIOimport timetime.sleep(1)import tkinter as tkGPIO.setwarnings(False)GPIO.setmode(GPIO.BCM)#setup output pins for relay controlGPIO.setup(9, GPIO.OUT) #power head 1 ch 4 relay#setup tkinterroot=tk.Tk()root.title("trial Aquarium")root.geometry("800x550")root.configure(bg="lightblue")photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file namelabel1 = tk.Label(image=photo1)#puts label in the window in this case not text file must be in program folderlabel1.grid(row=10, column=0, columnspan=12) #says how to place the label#setup fontsftlab= 'Verdana', 13, 'bold'ftb= 'Verdana', 11, 'bold'#define functions for button def pwrhd2on(): GPIO.output(9, GPIO.HIGH)def pwrhd2off(): GPIO.output(9, GPIO.LOW)state = GPIO.input(9)if state: rt2=('On')else: rt2=('Off')#setup exit buttonExitbutton= tk.Button(root, text="Exit", font=(ftb), width=6, bg="red", fg="white", command=root.destroy)Exitbutton.place(x=700, y=240)#setup powerhead 2 buttons and labellabelpwrhd2= 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)labelpwrhd2state= tk.Label(root, text=rt2, font=(ftlab), bg="green", fg="black")labelpwrhd2state.place(x=570, y=496)root.mainloop()
tkinter gpio 狀態(tài)問題.. 我怎樣才能讓它更新標(biāo)簽以顯示最新狀態(tài)
哆啦的時(shí)光機(jī)
2022-11-09 16:22:42