Python无法从一个类继承属性到另一个类

时间:2019-03-29 12:52:06

标签: python python-3.x pyqt

我一直在尝试将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):
                pass
class GUI(QMainWindow, Ui_MainWindow, GetData):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.Run_pushButton.setEnabled(True)
        self.Run_pushButton.clicked.connect(self.btn_run)

    def Display_data(self):
        self.thread = GetData(self)
        self.thread.ChangedData.connect(self.onDataChanged)
        self.thread.start()
        self.Stop_pushButton.setEnabled(True)
        self.Stop_pushButton.clicked.connect(self.btn_stop)

    def onDataChanged(self, Pwm, Distance, ArduinoTime, RunTime):
        self.Humid_lcdNumber_2.display(Pwm)
        self.Velocity_lcdNumber_3.display(Distance)
        self.Pwm_lcdNumber_4.display(ArduinoTime)
        self.Pressure_lcdNumber_5.display(RunTime)
        self.widget_2.update_plot(Pwm, RunTime)

    def btn_run(self):
        p1 = 1
        p2 = self.InputPos_Slider.value()
        the_bytes = bytes(f'<{p1},{p2}>\n', 'utf-8')
        a = self.Arduino.write(the_bytes)


    def btn_stop(self):
        p1 = 0
        p2 = 0
        the_bytes = bytes(f'<{p1},{p2}>\n', 'utf-8')
        a = self.Arduino.write(the_bytes)

这给了我这个错误,我以为我可以继承self.arduino,但是没有成功。

Traceback (most recent call last):
  File "C:\Users\Emman\Desktop\5th year\1THESIS\Python Program\C2_Final_PID.py", line 373, in btn_run
    self.Arduino.write(the_bytes)
AttributeError: 'GUI' object has no attribute 'Arduino'  

1 个答案:

答案 0 :(得分: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做同样的事情。)