亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 不能將屬性從一個類繼承到另一個類

Python 不能將屬性從一個類繼承到另一個類

LEATH 2021-12-21 17:32:43
我一直在嘗試將 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):
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

看起來您忘記了初始化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。)


查看完整回答
反對 回復 2021-12-21
  • 1 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號