我会研究你给我的信息。但是为了在这里继续讨论,代码的一部分正在放慢速度:
while self.startButton.isChecked():
self.widget.canvas.ax2.clear()
self.widget.canvas.ax2.set_xlabel('Sensor #')
self.widget.canvas.ax2.set_yscale('log', basey = 10)
self.widget.canvas.ax2.set_xlim(0, num_sensors + 1)
self.widget.canvas.ax2.set_xticks(range(1, num_sensors + 1))
self.widget.canvas.ax2.set_xticklabels(sensorLabel, fontsize = 10)
measure, read_s = [], []
t = round((time() - initialTime), 1)
for i in range(num_sensors):
read = self.ser.readline().strip()
measure.append(read)
read = float(read)
read_s.append(read)
self.widget.canvas.ax.plot(t, read, plot_array[i])
self.widget.canvas.ax2.scatter(range(1, num_sensors + 1), read_s, c = log10(read_s), s = 100)
self.widget.canvas.ax2.set_ylim(bottom = .8 * min(read_s))
f.write(str(t) + '\t' + '\t'.join(measure) + '\n')
self.widget.canvas.ax.legend(loc = 'center left', bbox_to_anchor = (2.2, 0.5), ncol = 1, fontsize = 10)
self.widget.canvas.draw()
QtCore.QCoreApplication.processEvents()
sleep(.5) # HERE IS THE SLEEP INTRODUCED
else:
global last_read
last_read = measure
self.statusbar.showMessage('Idle')
f.close()
self.ser.write('C')
try:
for i in range(1000):
self.ser.timeout = .1
a = self.ser.readline()
self.statusbar.showMessage('Deleting reads in the serial buffer')
if a == 'Last':
break
except:
self.ser.close()
GUI 运行得很好(使用少于 2% 的 CPU),直到我在按下 startButton 时调用这部分代码(在我再次按下它之前一直处于选中状态)。