This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_()) |
No comments:
Post a Comment