我一直在嘗試將 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。)
添加回答
舉報
0/150
提交
取消