-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_wrapper.py
More file actions
112 lines (91 loc) · 3.05 KB
/
Copy paththread_wrapper.py
File metadata and controls
112 lines (91 loc) · 3.05 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys, time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def exec_time(object):
print "exec time"
return object
class GuiThread(QThread):
def __init__(self, method, args=None, exec_times=None):
super(GuiThread, self).__init__()
self.method = method
self.args = args
self.parent = None
self.run = True
self.exec_times = exec_times
def set_args(self, args):
self.args = args
def stop(self):
self.run = False
def run(self):
exec_times = self.exec_times
self.run = True
print "starting thread type {} name {}".format(type(self.method), self.method.__name__)
while self.run:
self.method(*self.args)
time.sleep(0.5)
self.exec_times -= 1 if self.exec_times else None
if self.exec_times == 0:
self.stop()
self.exec_times = exec_times
def thread_this_method(execs=None):
print "creating decorator"
def wrap1(method):
def wrapper(*args, **kwargs):
instance = args[0]
print "Type of {} is {}".format(method.__name__, type(instance.__getattribute__(method.__name__)))
thread = GuiThread(method, exec_times=execs)
thread.set_args(args)
instance.__setattr__(method.__name__, thread.start)
instance.__getattribute__(method.__name__)()
return wrapper
return wrap1
class Form(QDialog):
sig = pyqtSignal()
def __init__(self, parent=None):
super(Form, self).__init__(parent)
layout = QVBoxLayout()
self.b1 = QPushButton("Button1")
self.b1.setCheckable(True)
self.b1.toggle()
self.b1.clicked.connect(lambda: self.whichbtn(self.b1))
self.b1.clicked.connect(self.btnstate)
layout.addWidget(self.b1)
self.b2 = QPushButton()
self.b2.setIcon(QIcon(QPixmap("python.gif")))
self.b2.clicked.connect(lambda: self.whichbtn(self.b2))
layout.addWidget(self.b2)
self.setLayout(layout)
self.b3 = QPushButton("Disabled")
self.b3.setEnabled(False)
layout.addWidget(self.b3)
self.b4 = QPushButton("&Default")
self.b4.setDefault(True)
self.b4.clicked.connect(lambda: self.whichbtn(self.b4))
layout.addWidget(self.b4)
self.setWindowTitle("Button demo")
self.sig.connect(self.myslot)
self.bt4state = True
@thread_this_method(execs=10)
def bt4toggle(self):
self.b4.setDown(self.bt4state)
self.bt4state ^= True
def btnstate(self):
self.sig.emit()
if self.b1.isChecked():
print "button pressed"
else:
print "button released"
def whichbtn(self, b):
print "clicked button is " + b.text()
def myslot(self):
#self.bt4toggle.set_parent(self)
self.bt4toggle()
def myslot2(self):
print "my slot2"
def main():
app = QApplication(sys.argv)
ex = Form()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()