Skip to content

Commit ccfe742

Browse files
committed
完善测试代码
1 parent 07c31ab commit ccfe742

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

Chapter07/qt07_connSlotsByName.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@
88
"""
99

1010
from PyQt5 import QtCore
11-
from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton
11+
from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton
1212
import sys
1313

14-
class CustWidget( QWidget):
14+
class CustWidget( QWidget ):
1515

1616
def __init__(self, parent=None):
1717
super(CustWidget, self).__init__(parent)
1818

1919
self.okButton = QPushButton("OK", self)
2020
#使用setObjectName设置对象名称
21-
self.okButton.setObjectName("okButton")
22-
21+
self.okButton.setObjectName("okButton")
2322
layout = QHBoxLayout()
2423
layout.addWidget(self.okButton)
25-
self.setLayout(layout)
26-
24+
self.setLayout(layout)
2725
QtCore.QMetaObject.connectSlotsByName(self)
2826

2927
@QtCore.pyqtSlot()
@@ -33,6 +31,6 @@ def on_okButton_clicked(self):
3331

3432
if __name__ == "__main__":
3533
app = QApplication(sys.argv)
36-
demo = CustWidget()
37-
demo.show()
34+
win = CustWidget()
35+
win.show()
3836
sys.exit(app.exec_())

Chapter07/qt07_signalSlot05.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
【简介】
5+
自定义信号和内置槽示例
6+
7+
8+
"""
9+
10+
from PyQt5.QtWidgets import QApplication ,QWidget, QPushButton
11+
from PyQt5.QtCore import pyqtSignal
12+
import sys
13+
14+
class WinForm(QWidget):
15+
button_clicked_signal = pyqtSignal() # 自定义信号,不带参
16+
17+
def __init__(self, parent = None):
18+
super(WinForm,self).__init__(parent)
19+
self.resize(330, 50 )
20+
self.setWindowTitle('自定义信号和内置槽示例')
21+
22+
btn = QPushButton('关闭', self)
23+
# 连接信号/槽
24+
btn.clicked.connect(self.btn_clicked)
25+
# 接收信号,连接到槽
26+
self.button_clicked_signal.connect(self.close)
27+
28+
def btn_clicked(self):
29+
# 发送无参数的自定义信号
30+
self.button_clicked_signal.emit()
31+
32+
if __name__ == '__main__':
33+
app = QApplication(sys.argv)
34+
win = WinForm()
35+
win.show()
36+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)