SOS - 将值从一个类打印到另一个类

时间:2017-08-20 06:36:14

标签: python multithreading class dynamic while-loop

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()

我有两个不同的类CountUp运行while循环和Scoreboard显示数字。现在我试图将Count中的值从CountUp类打印到Scoreboard类中:

scoreLcd.display(x)

给出了一个错误,即x未定义。这是因为我试图从另一个类中获取值。

所以现在问题是如何从类记分板

访问x的值

1 个答案:

答案 0 :(得分:0)

enter image description here

就像图片一样,您可以在父类中创建一个本机变量

  

1.定义一个可以返回值的函数,只需要返回X

中的set_value      

2.定义包含类X的类,例如Y

     

3.因为X是Y的父级,所以现在我们可以直接使用X的函数