Last active
April 10, 2018 08:57
-
-
Save thijstriemstra/c75830a9f30c1000e31bbf4a0940caaa to your computer and use it in GitHub Desktop.
pyqt async sevensegment example using quamash.QEventLoop and max7219
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
import sys | |
import time | |
import asyncio | |
from PyQt5.QtWidgets import (QApplication, QProgressBar, QMainWindow, QSlider, | |
QVBoxLayout, QWidget) | |
from quamash import QEventLoop, QThreadExecutor | |
import max7219.led as led | |
class MainWindow(QMainWindow): | |
def __init__(self): | |
super(MainWindow, self).__init__() | |
self.progress = QProgressBar() | |
self.progress.setRange(0, 99) | |
self.progress.show() | |
self.slider = QSlider() | |
self.slider.show() | |
# set layout | |
self.layout = QVBoxLayout() | |
self.layout.addWidget(self.progress) | |
self.layout.addWidget(self.slider) | |
self.win = QWidget() | |
self.win.resize(320, 240) | |
self.win.setLayout(self.layout) | |
self.win.show() | |
self.device = led.sevensegment() | |
self.device.clear() | |
with loop: | |
# context manager calls .close() when loop completes, | |
# and releases all resources | |
loop.run_until_complete(self.master()) | |
@asyncio.coroutine | |
def master(self): | |
yield from self.first_50() | |
with QThreadExecutor(1) as exec: | |
yield from loop.run_in_executor(exec, self.last_50) | |
print('ready!') | |
def updateValue(self, val): | |
self.progress.setValue(val) | |
self.device.write_text(0, str(val)) | |
@asyncio.coroutine | |
def first_50(self): | |
for i in range(50): | |
self.updateValue(i) | |
yield from asyncio.sleep(.1) | |
def last_50(self): | |
for i in range(50, 100): | |
loop.call_soon_threadsafe(self.updateValue, i) | |
time.sleep(.1) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
loop = QEventLoop(app) | |
asyncio.set_event_loop(loop) | |
main = MainWindow() | |
main.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment