I am rather new in GUI programming and multithreading apps. It is just a serial port monitor which plot serial data via pyqtgraph. I get annoying warning in console (QPixmap: It is not safe to use pixmaps outside the GUI thread), but I don't understand how to resolve it. Also I have invalid memory access (program exit with 139). How can I improve this code in general? I use Python3.5
from PyQt4 import QtGui
import sys, glob, serial, pyqtgraph, threading
import ui_main
import numpy as np
class GraphPlotter(QtGui.QMainWindow, ui_main.Ui_GraphPlotter):
def __init__(self):
pyqtgraph.setConfigOption('background', 'w')
super().__init__()
self.setupUi(self)
self.graphicsView.plotItem.showGrid(True, True, 0.7)
self.comboBox.addItems(serial_ports())
self.y = [0]
def update(self):
try:
c = pyqtgraph.hsvColor(0.5, alpha=.5)
pen = pyqtgraph.mkPen(color=c, width=3)
self.graphicsView.plot(np.arange(len(self.y)), self.y, pen=pen, clear=True)
except Exception:
print("""Can't update graph""")
class SerialMonitor:
def __init__(self):
self.running = True
self.form = GraphPlotter()
self.form.show()
self.thread = threading.Thread(target=self.serial_monitor_thread)
self.thread.start()
def serial_monitor_thread(self):
while self.running is True:
ser = serial.Serial('/dev/ttyS0', 115200)
msg = ser.readline()
if msg:
try:
self.form.y.append(int(msg))
self.form.update()
except ValueError:
print('Wrong data')
else:
pass
ser.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
monitor = SerialMonitor()
app.exec_()