|
1 | 1 | import asyncio
|
2 |
| -import functools |
3 | 2 | import sys
|
4 | 3 |
|
5 | 4 | import aiohttp
|
6 | 5 |
|
7 |
| -# from PyQt5.QtWidgets import ( |
8 |
| -from PySide2.QtWidgets import ( |
9 |
| - QWidget, |
| 6 | +# from PyQt6.QtWidgets import ( |
| 7 | +from PySide6.QtWidgets import ( |
| 8 | + QApplication, |
10 | 9 | QLabel,
|
11 | 10 | QLineEdit,
|
12 |
| - QTextEdit, |
13 | 11 | QPushButton,
|
| 12 | + QTextEdit, |
14 | 13 | QVBoxLayout,
|
| 14 | + QWidget, |
15 | 15 | )
|
16 |
| - |
17 |
| -import qasync |
18 |
| -from qasync import asyncSlot, asyncClose, QApplication |
| 16 | +from qasync import QEventLoop, asyncClose, asyncSlot |
19 | 17 |
|
20 | 18 |
|
21 | 19 | class MainWindow(QWidget):
|
22 | 20 | """Main window."""
|
23 | 21 |
|
24 |
| - _DEF_URL = "https://jsonplaceholder.typicode.com/todos/1" |
25 |
| - """str: Default URL.""" |
26 |
| - |
27 |
| - _SESSION_TIMEOUT = 1.0 |
28 |
| - """float: Session timeout.""" |
| 22 | + _DEF_URL: str = "https://jsonplaceholder.typicode.com/todos/1" |
| 23 | + """Default URL.""" |
29 | 24 |
|
30 | 25 | def __init__(self):
|
31 | 26 | super().__init__()
|
32 | 27 |
|
33 | 28 | self.setLayout(QVBoxLayout())
|
34 | 29 |
|
35 |
| - self.lblStatus = QLabel("Idle", self) |
36 |
| - self.layout().addWidget(self.lblStatus) |
| 30 | + self.lbl_status = QLabel("Idle", self) |
| 31 | + self.layout().addWidget(self.lbl_status) |
37 | 32 |
|
38 |
| - self.editUrl = QLineEdit(self._DEF_URL, self) |
39 |
| - self.layout().addWidget(self.editUrl) |
| 33 | + self.edit_url = QLineEdit(self._DEF_URL, self) |
| 34 | + self.layout().addWidget(self.edit_url) |
40 | 35 |
|
41 |
| - self.editResponse = QTextEdit("", self) |
42 |
| - self.layout().addWidget(self.editResponse) |
| 36 | + self.edit_response = QTextEdit("", self) |
| 37 | + self.layout().addWidget(self.edit_response) |
43 | 38 |
|
44 |
| - self.btnFetch = QPushButton("Fetch", self) |
45 |
| - self.btnFetch.clicked.connect(self.on_btnFetch_clicked) |
46 |
| - self.layout().addWidget(self.btnFetch) |
| 39 | + self.btn_fetch = QPushButton("Fetch", self) |
| 40 | + self.btn_fetch.clicked.connect(self.on_btn_fetch_clicked) |
| 41 | + self.layout().addWidget(self.btn_fetch) |
47 | 42 |
|
48 |
| - self.session = aiohttp.ClientSession( |
49 |
| - loop=asyncio.get_event_loop(), |
50 |
| - timeout=aiohttp.ClientTimeout(total=self._SESSION_TIMEOUT), |
51 |
| - ) |
| 43 | + self.session: aiohttp.ClientSession |
52 | 44 |
|
53 | 45 | @asyncClose
|
54 |
| - async def closeEvent(self, event): |
| 46 | + async def closeEvent(self, event): # noqa:N802 |
55 | 47 | await self.session.close()
|
56 | 48 |
|
| 49 | + async def boot(self): |
| 50 | + self.session = aiohttp.ClientSession() |
| 51 | + |
57 | 52 | @asyncSlot()
|
58 |
| - async def on_btnFetch_clicked(self): |
59 |
| - self.btnFetch.setEnabled(False) |
60 |
| - self.lblStatus.setText("Fetching...") |
| 53 | + async def on_btn_fetch_clicked(self): |
| 54 | + self.btn_fetch.setEnabled(False) |
| 55 | + self.lbl_status.setText("Fetching...") |
61 | 56 |
|
62 | 57 | try:
|
63 |
| - async with self.session.get(self.editUrl.text()) as r: |
64 |
| - self.editResponse.setText(await r.text()) |
| 58 | + async with self.session.get(self.edit_url.text()) as r: |
| 59 | + self.edit_response.setText(await r.text()) |
65 | 60 | except Exception as exc:
|
66 |
| - self.lblStatus.setText("Error: {}".format(exc)) |
| 61 | + self.lbl_status.setText("Error: {}".format(exc)) |
67 | 62 | else:
|
68 |
| - self.lblStatus.setText("Finished!") |
| 63 | + self.lbl_status.setText("Finished!") |
69 | 64 | finally:
|
70 |
| - self.btnFetch.setEnabled(True) |
71 |
| - |
72 |
| - |
73 |
| -async def main(): |
74 |
| - def close_future(future, loop): |
75 |
| - loop.call_later(10, future.cancel) |
76 |
| - future.cancel() |
| 65 | + self.btn_fetch.setEnabled(True) |
77 | 66 |
|
78 |
| - loop = asyncio.get_event_loop() |
79 |
| - future = asyncio.Future() |
80 | 67 |
|
81 |
| - app = QApplication.instance() |
82 |
| - if hasattr(app, "aboutToQuit"): |
83 |
| - getattr(app, "aboutToQuit").connect( |
84 |
| - functools.partial(close_future, future, loop) |
85 |
| - ) |
86 |
| - |
87 |
| - mainWindow = MainWindow() |
88 |
| - mainWindow.show() |
| 68 | +if __name__ == "__main__": |
| 69 | + app = QApplication(sys.argv) |
89 | 70 |
|
90 |
| - await future |
91 |
| - return True |
| 71 | + event_loop = QEventLoop(app) |
| 72 | + asyncio.set_event_loop(event_loop) |
92 | 73 |
|
| 74 | + app_close_event = asyncio.Event() |
| 75 | + app.aboutToQuit.connect(app_close_event.set) |
| 76 | + |
| 77 | + main_window = MainWindow() |
| 78 | + main_window.show() |
93 | 79 |
|
94 |
| -if __name__ == "__main__": |
95 |
| - try: |
96 |
| - qasync.run(main()) |
97 |
| - except asyncio.exceptions.CancelledError: |
98 |
| - sys.exit(0) |
| 80 | + event_loop.create_task(main_window.boot()) |
| 81 | + event_loop.run_until_complete(app_close_event.wait()) |
| 82 | + event_loop.close() |
0 commit comments