Skip to content

Instantly share code, notes, and snippets.

@Willy-JL
Last active July 28, 2021 00:22
Show Gist options
  • Save Willy-JL/db9d9e492025d5ec43ab4ce8baa82c15 to your computer and use it in GitHub Desktop.
Save Willy-JL/db9d9e492025d5ec43ab4ce8baa82c15 to your computer and use it in GitHub Desktop.
Basic PyQt GUI impementing asyncio behavior
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