Python SOS-将值从一个类打印到另一个类
我有两个不同的类:运行while循环的倒计时和显示数字的记分板。现在我尝试将x中的值从CountUp类打印到Scoreboard类,如下所示:Python SOS-将值从一个类打印到另一个类,python,multithreading,class,dynamic,while-loop,Python,Multithreading,Class,Dynamic,While Loop,我有两个不同的类:运行while循环的倒计时和显示数字的记分板。现在我尝试将x中的值从CountUp类打印到Scoreboard类,如下所示: import sys from PyQt5.QtWidgets import QWidget, QLabel, QApplication,QLCDNumber, QVBoxLayout from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread, QThreadPool
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication,QLCDNumber, QVBoxLayout
from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread, QThreadPool, pyqtSignal)
from PyQt5 import QtGui
import time
class Scoreboard(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
scoreLcd = QLCDNumber(self)
scoreLcd.display(x) #How to grab values in x below to display here
self.setGeometry(10, 50, 100, 100)
self.setWindowTitle('TIMER')
self.show()
class CountUp(QThread):
def run(self):
x = 0
while 1:
x = x + 1
time.sleep(1)
print (x) #I want the value in x to be printed in the GUI above
def Counter():
app = QApplication(sys.argv)
ex = Scoreboard()
thread = CountUp()
thread.finished.connect(app.exit)
thread.start()
sys.exit(app.exec_())
if __name__ == '__main__':
Counter()
这给了我一个错误,x没有定义。这是因为我试图从另一个类中获取值
现在的问题是如何从课堂记分板上获取x的值
正如图所示,您可以在父类中创建本机变量
1.定义一个可以返回值的函数,只返回X中的set_值
2.定义一个包含类X的类,例如Y
3.因为X是Y的母函数,所以现在我们可以直接使用X的函数
Errrr,很简单。你只需要2用arduinoData替换self.x,然后在你的chid类中使用它。因为我的电脑中没有pyQT,所以你自己做吧,可能是版本问题,我使用python2.7,程序运行正常。我记得super在2.7和3.6之间有所不同,
scoreLcd.display(x)