gpt4 book ai didi

python - LED闪烁如何打发时间。 sleep RPI

转载 作者:行者123 更新时间:2023-12-03 13:02:31 30 4
gpt4 key购买 nike

我在qtdesigner中设计了一个表单。它具有“开”和“关”按钮。开按钮应该开始闪烁,而关按钮应该停止它。因此,如果time.sleep持续时间很短,那没有问题,但是当我写10秒钟的 sleep 时间时,当我单击关闭按钮时它不会立即停止。程序等待10秒以停止Led闪烁。那么time.sleep如何被打断呢?


import time
import threading
import RPi.GPIO as GPIO
import sys
from time import sleep
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

switch = True


def blink(self):
def run():
while (switch == True):
print('BLINK...BLINK...')
GPIO.output(17, GPIO.HIGH)
time.sleep(10.0)
GPIO.output(17, GPIO.LOW)
time.sleep(10.0)
if switch == False:
break
thread = threading.Thread(target=run)
thread.start()



class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)


self.pshbttn1 = QtWidgets.QPushButton(Form)
self.pshbttn1.setGeometry(QtCore.QRect(60, 170, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn1.setFont(font)
self.pshbttn1.setObjectName("pshbttn1")
self.pshbttn1.clicked.connect(self.switchon)


self.pshbttn2 = QtWidgets.QPushButton(Form)
self.pshbttn2.setGeometry(QtCore.QRect(220, 170, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn2.setFont(font)
self.pshbttn2.setObjectName("pshbttn2")
self.pshbttn2.clicked.connect(self.switchoff)


self.pshbttn3 = QtWidgets.QPushButton(Form)
self.pshbttn3.setGeometry(QtCore.QRect(140, 230, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn3.setFont(font)
self.pshbttn3.setObjectName("pshbttn3")
self.pshbttn3.clicked.connect(app.exit)


self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 80, 251, 51))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "LED"))
self.pshbttn1.setText(_translate("Form", "ON"))
self.pshbttn2.setText(_translate("Form", "OFF"))
self.pshbttn3.setText(_translate("Form", "EXIT"))
self.label.setText(_translate("Form", "LED\'i açmak için butonları kullanın"))

def switchon(self):
global switch
switch = True
print ('switch on')
blink(self)

def switchoff(self):
print ('switch off')
global switch
switch = False

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

最佳答案

在这种情况下,不必使用 sleep ,只需使用QTimer。为了简化任务,我创建了一个处理销钉的类。另外,PyQt5建议不要修改Qt Designer生成的类。

import sys

from PyQt5 import QtCore, QtGui, QtWidgets

import RPi.GPIO as GPIO


class Led:
def __init__(self, pin, timeout=1000):
self._state = False
self._pin = pin
self._timeout = timeout

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin, GPIO.OUT)

self.blink_timer = QtCore.QTimer(
interval=self.timeout, timeout=self._on_blink_timeout
)

def _on_blink_timeout(self):
self.state = not self.state

def _update_internal_state(self):
GPIO.output(self.pin, GPIO.HIGH if self._state else GPIO.LOW)

@property
def pin(self):
return self._pin

@property
def timeout(self):
return self._timeout

@timeout.setter
def timeout(self, v):
self._timeout = v
is_active = self.blink_timer.isActive()
self.blink_timer.setInterval(self.timeout)
if is_active:
self.blink_timer.start()

def on(self):
self.state = True

def off(self):
self.state = False

@property
def state(self):
return self._state

@state.setter
def state(self, s):
self._state = s
self._update_internal_state()

def start(self):
self.state = True
self.blink_timer.start()

def stop(self):
self.state = False
self.blink_timer.stop()


class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)

self.pshbttn1 = QtWidgets.QPushButton(Form)
self.pshbttn1.setGeometry(QtCore.QRect(60, 170, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn1.setFont(font)
self.pshbttn1.setObjectName("pshbttn1")
self.pshbttn2 = QtWidgets.QPushButton(Form)
self.pshbttn2.setGeometry(QtCore.QRect(220, 170, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn2.setFont(font)
self.pshbttn2.setObjectName("pshbttn2")
self.pshbttn3 = QtWidgets.QPushButton(Form)
self.pshbttn3.setGeometry(QtCore.QRect(140, 230, 125, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pshbttn3.setFont(font)
self.pshbttn3.setObjectName("pshbttn3")
self.pshbttn3.clicked.connect(app.exit)

self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 80, 251, 51))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "LED"))
self.pshbttn1.setText(_translate("Form", "ON"))
self.pshbttn2.setText(_translate("Form", "OFF"))
self.pshbttn3.setText(_translate("Form", "EXIT"))
self.label.setText(_translate("Form", "LED'i açmak için butonları kullanın"))


class Widget(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setupUi(self)
self.led = Led(17, timeout=10000)
self.pshbttn1.clicked.connect(self.led.start)
self.pshbttn2.clicked.connect(self.led.stop)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

关于python - LED闪烁如何打发时间。 sleep RPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59431441/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com