每次线程t1每秒调用wait\u thread\u v1函数时,我的python脚本都需要更改一个对象lcd\u p1,但是如何做到这一点呢?我不知道如何在函数中访问此对象?有人能帮忙吗
vazao1 = 12
global pulses_v1
pulses_v1 = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(vazao1, GPIO.IN)
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(50, 50, 640, 480)
self.setWindowTitle("Programa")
self.initUI()
def initUI(self):
self.lcd_v1 = QLCDNumber(self)
self.lcd_v1.display(0)
self.lcd_v1.setDigitCount(4)
self.lcd_v1.setFixedWidth(180)
self.lcd_v1.setFixedHeight(80)
self.lcd_v1.move(60,320)
def count_pulses_v1(channel):
global pulses_v1
pulses_v1 += 1
def wait_thread_v1():
global pulses_v1
while True:
time.sleep(1)
print("Pulsos total de vazão 1: "+str(pulses_v1))
#Window.initUI.self.lcd_p1.display(100)
pulses_v1 = 0
GPIO.add_event_detect(vazao1, GPIO.FALLING, callback=count_pulses_v1)
t1 = Thread(target=wait_thread_v1, name='thread_01', args=())
t1.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
您不能也不应该从辅助线程修改GUI,如the docs所示,因为除了不需要创建新线程之外,Qt不保证它可以工作。在这种情况下,最好创建一个QObject,在调用回调时发出信号,因为它是线程安全的,然后将该信号连接到增加脉冲的插槽,然后使用QTimer实现线程的逻辑
相关问题 更多 >
编程相关推荐