forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignalsExample.py
65 lines (50 loc) · 1.96 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年7月2日
@author: Irony
@site: https://pyqt5.com https://github.com/PyQt5
@email: [email protected]
@file: QPushButton.SignalsExample
@description: 按钮信号例子
"""
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QPlainTextEdit
__Author__ = "Irony"
__Copyright__ = "Copyright (c) 2019"
__Version__ = "Version 1.0"
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)
layout.addWidget(QPushButton(
'按钮按下信号', self, objectName='PressBtn', pressed=self.onPressed))
layout.addWidget(QPushButton(
'按钮释放信号', self, objectName='ReleaseBtn', released=self.onReleased))
layout.addWidget(QPushButton(
'按钮选中信号', self, checkable=True, objectName='ToggleBtn', toggled=self.onToggled))
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
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())