我想要的是PyQt5标签(电源按钮状态)中的GUI根据物理按钮(开/关按钮-表示电源状态)的位置进行相应更新
问题是。。。代码在我看来很好,但当我运行时,它只运行一次。。。。它会根据按钮位置相应地显示颜色和文本,但之后程序会关闭
这是我的节目
希望你能帮我一把
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys, time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
powerButton = 1 # button position in RPi - gpio1
GPIO.setup(powerButton, GPIO.IN)
class MyWindow(QDialog):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 300, 300)
self.setWindowTitle("On/Off Color variation")
self.InitUi()
self.show()
def InitUi(self):
self.label = QtWidgets.QLabel(self)
if GPIO.input(powerButton) == GPIO.HIGH:
self.label.setText("Power On")
self.label.setStyleSheet("background-color: lightgreen")
elif GPIO.input(powerButton) == GPIO.LOW:
self.label.setText("Power Off")
self.label.setStyleSheet("background-color: red")
self.powerStatus = powerStatusThread()
self.powerStatus.start()
class powerStatusThread(QThread):
def run(self):
while True:
if GPIO.input(powerButton) == GPIO.HIGH:
print("Button ON")
self.label.setText("Power On")
self.label.setStyleSheet("background-color: lightgreen")
self.label.update()
elif GPIO.input(powerButton) == GPIO.LOW:
print("BUTTON OFF")
self.label.setText("Power Off")
self.label.setStyleSheet("background-color: red")
self.label.update()
time.sleep(2)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainDialog = MyWindow()
mainDialog.show()
sys.exit(app.exec_())
目前没有回答
相关问题 更多 >
编程相关推荐