-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
73 lines (55 loc) · 1.92 KB
/
example.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sloop
import asyncio
import random
from PyQt5 import QtCore, QtWidgets
sloop.AUTO_CLOSE = False
sloop.DAEMON = True
class MainWindow(QtWidgets.QMainWindow):
result_signal = QtCore.pyqtSignal(object)
def __init__(self, parent=None):
super().__init__(parent)
layout = QtWidgets.QVBoxLayout()
self.button = QtWidgets.QPushButton("Press me")
layout.addWidget(self.button)
self.label1 = QtWidgets.QLabel("0")
self.label2 = QtWidgets.QLabel("0")
self.label3 = QtWidgets.QLabel("0")
self.label4 = QtWidgets.QLabel("0")
lay2 = QtWidgets.QHBoxLayout()
lay2.addWidget(self.label1)
lay2.addWidget(self.label2)
lay2.addWidget(self.label3)
lay2.addWidget(self.label4)
layout.addLayout(lay2)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.button.clicked.connect(self.button_click)
# self.button.clicked.connect(self.button_click2)
self.result_signal.connect(self._handle_result)
@sloop.wrap_in_thread()
def button_click2(self, *args):
import time
for i in range(10):
if not sloop.alt.is_alive():
return
time.sleep(random.random())
label = random.choice([self.label1, self.label2, self.label3, self.label4])
self.result_signal.emit(label)
@sloop.wrap_coro()
async def button_click(self, *args, **kwargs):
await asyncio.sleep(random.random() * 10)
label = random.choice([self.label1, self.label2, self.label3, self.label4])
self.result_signal.emit(label)
def _handle_result(self, data):
text = int(data.text())
text += 1
data.setText(str(text))
def main():
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()
print("here")
if __name__ == '__main__':
main()