Interop between asyncio and Qt for Python
Project description
qtinter — Interop between asyncio and Qt for Python
qtinter
is a Python module that brings together asyncio and Qt
for Python, allowing you to use one from the other seamlessly.
Read the full documentation or check out the quickstart below.
Installation
$ pip install qtinter
Using asyncio from Qt
To use asyncio-based libraries in Qt for Python, enclose app.exec()
inside context manager qtinter.using_asyncio_from_qt()
.
Example (taken from examples/clock.py
):
"""Display LCD-style digital clock"""
import asyncio
import datetime
import qtinter # <-- import module
from PySide6 import QtWidgets
class Clock(QtWidgets.QLCDNumber):
def __init__(self, parent=None):
super().__init__(parent)
self.setDigitCount(8)
def showEvent(self, event):
self._task = asyncio.create_task(self._tick())
def hideEvent(self, event):
self._task.cancel()
async def _tick(self):
while True:
t = datetime.datetime.now()
self.display(t.strftime("%H:%M:%S"))
await asyncio.sleep(1.0 - t.microsecond / 1000000 + 0.05)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Clock()
widget.setWindowTitle("qtinter - Digital Clock example")
widget.resize(300, 50)
with qtinter.using_asyncio_from_qt(): # <-- enable asyncio in qt code
widget.show()
app.exec()
Using Qt from asyncio
To use Qt components from asyncio-based code, enclose the asyncio
entry-point inside context manager qtinter.using_qt_from_asyncio()
.
Example (taken from examples/color.py
):
"""Display the RGB code of a color chosen by the user"""
import asyncio
import qtinter # <-- import module
from PySide6 import QtWidgets
async def choose_color():
dialog = QtWidgets.QColorDialog()
dialog.show()
future = asyncio.Future()
dialog.finished.connect(future.set_result)
result = await future
if result == QtWidgets.QDialog.DialogCode.Accepted:
return dialog.selectedColor().name()
else:
return None
if __name__ == "__main__":
app = QtWidgets.QApplication([])
with qtinter.using_qt_from_asyncio(): # <-- enable qt in asyncio code
color = asyncio.run(choose_color())
if color is not None:
print(color)
Using modal dialogs
To execute a modal dialog without blocking the asyncio event loop,
wrap the dialog entry-point in qtinter.modal()
and await
on it.
Example (taken from examples/hit_100.py
):
import asyncio
import qtinter
from PySide6 import QtWidgets
async def main():
async def counter():
nonlocal n
while True:
print(f"\r{n}", end='', flush=True)
await asyncio.sleep(0.025)
n += 1
n = 0
counter_task = asyncio.create_task(counter())
await qtinter.modal(QtWidgets.QMessageBox.information)(
None, "Hit 100", "Click OK when you think you hit 100.")
counter_task.cancel()
if n == 100:
print("\nYou did it!")
else:
print("\nTry again!")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
with qtinter.using_qt_from_asyncio():
asyncio.run(main())
Requirements
qtinter
supports the following:
- Python version: 3.7 or higher
- Qt binding: PyQt5, PyQt6, PySide2, PySide6
- Operating system: Linux, MacOS, Windows
License
BSD License.
Contributing
Please raise an issue if you have any questions. Pull requests are more than welcome!
Credits
qtinter
is derived from
qasync but rewritten from
scratch. qasync is derived from
asyncqt, which is derived from
quamash.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file qtinter-0.11.0.tar.gz
.
File metadata
- Download URL: qtinter-0.11.0.tar.gz
- Upload date:
- Size: 320.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d4d9e4c3c3c6789a19c9b1c2e098491ed36f0a8d106162c2679dd05e460bffd |
|
MD5 | 6a2be1afd5d1e74ebb151729e6fe76f3 |
|
BLAKE2b-256 | a877617f55a7b73391b0d5387bf38074c5c231f782d4cbc4f8839a46588ce319 |
File details
Details for the file qtinter-0.11.0-py3-none-any.whl
.
File metadata
- Download URL: qtinter-0.11.0-py3-none-any.whl
- Upload date:
- Size: 27.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d4814cb2ddffb8138b618f8e95687e2c3144b76f095b5c9e3abe08090d773f1 |
|
MD5 | 243013dee5c6a9b90be7bf813ebd7d16 |
|
BLAKE2b-256 | 6e0d0602ee5f941bdf95aa3b53c245ab957565300c966d945bb122b5281bba8a |