Last active
July 28, 2021 00:22
-
-
Save Willy-JL/db9d9e492025d5ec43ab4ce8baa82c15 to your computer and use it in GitHub Desktop.
Basic PyQt GUI impementing asyncio behavior
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 PyQt5.QtWidgets import QProgressBar | |
from qasync import QApplication | |
import functools | |
import asyncio | |
import qasync | |
import sys | |
async def main(): | |
def close_future(future, loop): | |
loop.call_later(10, future.cancel) | |
future.cancel("Close Application") | |
future = asyncio.Future() | |
app = QApplication.instance() | |
if hasattr(app, 'aboutToQuit'): | |
getattr(app, 'aboutToQuit').connect(functools.partial(close_future, future, asyncio.get_event_loop())) | |
progress = QProgressBar() | |
progress.setRange(0, 99) | |
progress.show() | |
for i in range(100): | |
progress.setValue(i) | |
await asyncio.sleep(0.1) | |
await future | |
return True | |
if __name__ == "__main__": | |
try: | |
qasync.run(main()) | |
except asyncio.exceptions.CancelledError: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment