如何将我的 LCD 连接到 class 测试以便显示 x?
how is it possible to connect my LCD with the class test so that x is displayed?
我正在尝试创建一个 GUI 来显示从 Raspberry 获取的机器数据。
不幸的是,我无法更新我的 QT-Desinger 表面。
所以我现在正在尝试这个 "test class" 但遗憾的是没有成功
我已经有了。有些东西不见了...但我现在不知道是什么
x = 0
class Ui_Form(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.lcdNumber = QtWidgets.QLCDNumber(Form)
self.lcdNumber.setGeometry(QtCore.QRect(10, 50, 361, 191))
self.lcdNumber.setObjectName("lcdNumber")
self.lcdNumber.display(x)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
def run(self):
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
class Test(threading.Thread):
global x
def __init__(self):
threading.Thread.__init__(self)
def runs(self):
while x <= 20:
print(x)
x = x + 1
time.sleep(2)
t = Ui_Form()
t1 = Test()
t.start()
t1.start()
计数器显示 0,循环根本没有开始..
我的目标是让 LCD 不断自我更新 schow x
那可能吗 ?
谢谢
更新x的值,使用PyQt时QTimer是最好的方式,不需要使用threading模块
from PyQt5.Qt import QLCDNumber, QDialog, QPushButton, QVBoxLayout, QApplication,QTimer
import sys
class LCD(QDialog):
x = 0
def __init__(self):
super(LCD, self).__init__()
self.lcdNumber = QLCDNumber()
self.pushStart = QPushButton("Start")
self.pushStart.clicked.connect(self.update)
vBox = QVBoxLayout()
vBox.addWidget(self.lcdNumber)
vBox.addWidget(self.pushStart)
self.setLayout(vBox)
self.timer = QTimer()
self.timer.timeout.connect(self.update)
def update(self):
self.lcdNumber.display(str(self.x))
self.x += 1
self.timer.start(1000)
if __name__ == "__main__":
app = QApplication(sys.argv)
lcd = LCD()
lcd.show()
sys.exit(app.exec_())
我正在尝试创建一个 GUI 来显示从 Raspberry 获取的机器数据。
不幸的是,我无法更新我的 QT-Desinger 表面。
所以我现在正在尝试这个 "test class" 但遗憾的是没有成功
我已经有了。有些东西不见了...但我现在不知道是什么
x = 0
class Ui_Form(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.lcdNumber = QtWidgets.QLCDNumber(Form)
self.lcdNumber.setGeometry(QtCore.QRect(10, 50, 361, 191))
self.lcdNumber.setObjectName("lcdNumber")
self.lcdNumber.display(x)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
def run(self):
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
class Test(threading.Thread):
global x
def __init__(self):
threading.Thread.__init__(self)
def runs(self):
while x <= 20:
print(x)
x = x + 1
time.sleep(2)
t = Ui_Form()
t1 = Test()
t.start()
t1.start()
计数器显示 0,循环根本没有开始..
我的目标是让 LCD 不断自我更新 schow x 那可能吗 ?
谢谢
更新x的值,使用PyQt时QTimer是最好的方式,不需要使用threading模块
from PyQt5.Qt import QLCDNumber, QDialog, QPushButton, QVBoxLayout, QApplication,QTimer
import sys
class LCD(QDialog):
x = 0
def __init__(self):
super(LCD, self).__init__()
self.lcdNumber = QLCDNumber()
self.pushStart = QPushButton("Start")
self.pushStart.clicked.connect(self.update)
vBox = QVBoxLayout()
vBox.addWidget(self.lcdNumber)
vBox.addWidget(self.pushStart)
self.setLayout(vBox)
self.timer = QTimer()
self.timer.timeout.connect(self.update)
def update(self):
self.lcdNumber.display(str(self.x))
self.x += 1
self.timer.start(1000)
if __name__ == "__main__":
app = QApplication(sys.argv)
lcd = LCD()
lcd.show()
sys.exit(app.exec_())