Skip to content

Instantly share code, notes, and snippets.

@dean-shaff
Last active July 28, 2016 14:29
Show Gist options
  • Save dean-shaff/218372f2a7ca83dc77743e82b4731d3c to your computer and use it in GitHub Desktop.
Save dean-shaff/218372f2a7ca83dc77743e82b4731d3c to your computer and use it in GitHub Desktop.
from PyQt4 import QtCore, QtGui
from mainui import MainUI
import time
from hardware_controller import Hardware_Controller
class Worker(QtCore.QThread):
update_text_signal = QtCore.pyqtSignal(str) #have to pass the type str
def __init__(self, hdwr_ctrl, update_rate):
QtCore.QThread.__init__(self)
self.hdwr_ctrl = hdwr_ctrl
self.update_rate = update_rate
self.running = True
self.controller_num = 1
def run(self):
while(self.running):
text = self.hdwr_ctrl.get_important_text(self.controller_num)
update_text_signal.emit(text)
time.sleep(self.update_rate)
@QtCore.pyqtSlot(int)
def change_controller_num(self,i):
"""
Change the controller number from the spin box in the UI.
"""
self.controller_num = i
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.ui = MainUI # contains a bunch of widget definitions we don't care about
self.ui.setUpUi(self)
self.__connect()
def __connect(self):
self.hdwr_ctrl = Hardware_Controller()
self.worker = Worker(self.hdwr_ctrl,1)
self.worker.update_text_signal.connect(self.update_text)
QtCore.QObject.connect(self.ui.spinbox, QtCore.SINGAL("valueChanged(int)"), self.worker.change_controller_num)
self.worker.start()
# so we can close the program
self.connect(self, QtCore.SIGNAL('triggered()'), self.closeEvent)
def update_text(self, text):
"""
Callback for the worker.update_text_signal pyqtSignal
"""
self.ui.textBox.setText(text)
def closeEvent(self):
"""
Safely shut down worker threads.
"""
self.worker.running = False
self.worker.quit()
self.worker.wait()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
sys.exit(app_exec_())
@dean-shaff
Copy link
Author

Thanks to Paul for syntax correction in the closeEvent method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment