Python 不能将属性从一个类继承到另一个类

我一直在尝试将 self.Arduino 从 GetData 类继承到 GUI 类。所以为了做到这一点,我简单地添加了这行代码。


class GUI(QMainWindow, Ui_MainWindow, GetData):

我以为它会继承 self.Arduino 但它没有。显然我做错了什么,但我不明白是什么。这是我的代码


class GetData(QThread):

    ChangedData = pyqtSignal(float, float, float, float)


    def __init__(self, parent=None):

        QThread.__init__(self, parent)


        arduino_ports = [  # automatically searches for an Arduino and selects the port it's on

            p.device

            for p in serial.tools.list_ports.comports()

            if 'Arduino' in p.description

        ]


        if not arduino_ports:

            raise IOError("No Arduino found - is it plugged in? If so, restart computer.")

        if len(arduino_ports) > 1:

            warnings.warn('Multiple Arduinos found - using the first')

        self.Arduino = serial.Serial(arduino_ports[0], 9600, timeout=1)


    def __del__(self):  # part of the standard format of a QThread

        self.wait()




    def run(self):  # also a required QThread func tion, the working part

        import time

        self.Arduino.close()

        self.Arduino.open()


        self.Arduino.flush()

        self.Arduino.reset_input_buffer()

        start_time = time.time()


        while True:

            while self.Arduino.inWaiting() == 0:

                pass

            try:

                data = self.Arduino.readline()

                dataarray = data.decode().rstrip().split(',')

                self.Arduino.reset_input_buffer()

                Pwm = round(float(dataarray[0]), 3)

                Distance = round(float(dataarray[1]), 3)

                ArduinoTime = round(float(dataarray[2]), 3)

                RunTime = round(time.time() - start_time, 3)

                print(Pwm, 'Pulse', ",", Distance, 'CM', ",", ArduinoTime, "Millis", ",", RunTime, "Time Elasped")

                self.ChangedData.emit(Pwm, Distance, ArduinoTime , RunTime)

            except (KeyboardInterrupt, SystemExit, IndexError, ValueError):


LEATH
浏览 186回答 1
1回答

繁花如伊

看起来您忘记了初始化GetData:class GUI(QMainWindow, Ui_MainWindow, GetData):    def __init__(self, parent=None):        QMainWindow.__init__(self, parent)        GetData.__init__(self, parent)  # this is the missing line        self.setupUi(self)        self.Run_pushButton.setEnabled(True)        self.Run_pushButton.clicked.connect(self.btn_run)(请注意,您可能需要对 做同样的事情Ui_MainWindow。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python