我一直在尝试将 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
提交
取消