File: modal_example.py

package info (click to toggle)
qasync 0.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 336 kB
  • sloc: python: 1,849; makefile: 10
file content (47 lines) | stat: -rw-r--r-- 1,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import sys

# from PyQt6.QtWidgets import
from PySide6.QtWidgets import QApplication, QMessageBox, QProgressBar

from qasync import QEventLoop, asyncWrap


async def master():
    progress = QProgressBar()
    progress.setRange(0, 99)
    progress.show()
    await first_50(progress)


async def first_50(progress):
    for i in range(50):
        progress.setValue(i)
        await asyncio.sleep(0.1)

    # Schedule the last 50% to run asynchronously
    asyncio.create_task(last_50(progress))

    # create a notification box, use helper to make entering event loop safe.
    result = await asyncWrap(
        lambda: QMessageBox.information(
            None, "Task Completed", "The first 50% of the task is completed."
        )
    )
    assert result == QMessageBox.StandardButton.Ok


async def last_50(progress):
    for i in range(50, 100):
        progress.setValue(i)
        await asyncio.sleep(0.1)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    event_loop = QEventLoop(app)
    asyncio.set_event_loop(event_loop)

    event_loop.run_until_complete(master())
    event_loop.close()