After moving to NYC for the summer, I found myself in need of a new bike. The bike I rode across the country last summer was always a little too small for me, and after growing about half an inch in the last year, it really doesn't fit me anymore. I was going to bring a bike to NYC for commuting, but it got stolen out of my car when I was visiting San Francisco. Luckily the one that got stolen was kind of a piece of junk. It was a fixed gear strapped to a cheap origin8 frame. The one thing I liked (and now miss) were the larger volume tires and the wide mountain bike handlebars. There's something about rolling around on a fixed gear with wide-ass bars that just puts a smile on my face.
Wednesday, July 27, 2016
Tuesday, July 26, 2016
Multithreading in PyQt
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_()) |
Subscribe to:
Posts (Atom)