-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
SignalsExample.py
73 lines (56 loc) · 2.12 KB
/
SignalsExample.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年7月2日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: QPushButton.SignalsExample
@description: 按钮信号例子
"""
try:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QPlainTextEdit
except ImportError:
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QPlainTextEdit
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
btn1 = QPushButton('按钮点击信号', self)
btn1.setObjectName('ClickBtn')
btn1.clicked.connect(self.onClicked)
layout.addWidget(btn1)
btn2 = QPushButton('按钮按下信号', self)
btn2.setObjectName('PressBtn')
btn2.pressed.connect(self.onPressed)
layout.addWidget(btn2)
btn3 = QPushButton('按钮释放信号', self)
btn3.setObjectName('ReleaseBtn')
btn3.released.connect(self.onReleased)
layout.addWidget(btn3)
btn4 = QPushButton('按钮释放信号', self)
btn4.setObjectName('ToggleBtn')
btn4.setCheckable(True)
btn4.toggled.connect(self.onToggled)
layout.addWidget(btn4)
self.resultView = QPlainTextEdit(self)
self.resultView.setReadOnly(True)
layout.addWidget(self.resultView)
def onClicked(self):
self.resultView.appendPlainText(
'按钮{0}被点击'.format(self.sender().objectName()))
def onPressed(self):
self.resultView.appendPlainText(
'按钮{0}被按下'.format(self.sender().objectName()))
def onReleased(self):
self.resultView.appendPlainText(
'按钮{0}被释放'.format(self.sender().objectName()))
def onToggled(self, checked):
self.resultView.appendPlainText(
'按钮{0}被选中:{1}'.format(self.sender().objectName(), checked))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())