Skip to content

Commit 1b53a7a

Browse files
committedMay 3, 2017
部件中的信号槽通信示例
1 parent 9b8d76f commit 1b53a7a

6 files changed

+90
-93
lines changed
 
File renamed without changes.

‎Chapter07/qt07_widgetSignalSlot01.py

-21
This file was deleted.

‎Chapter07/qt07_winSignalSlot01.py

+21-39
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
1-
# -*- coding: utf-8 -*-
2-
3-
"""
4-
【简介】
5-
部件中的信号槽通信示例
6-
7-
8-
"""
9-
10-
11-
from PyQt5.QtWidgets import QMainWindow,QHBoxLayout, QPushButton , QApplication, QWidget
12-
import sys
13-
14-
class WinForm(QMainWindow):
15-
16-
def __init__(self, parent=None):
17-
super(WinForm, self).__init__(parent)
18-
self.setWindowTitle('部件中的信号槽通信')
19-
20-
self.button1 = QPushButton('Button 1')
21-
#
22-
self.button1.clicked.connect(self.onButtonClick)
23-
24-
layout = QHBoxLayout()
25-
layout.addWidget(self.button1)
26-
27-
main_frame = QWidget()
28-
main_frame.setLayout(layout)
29-
self.setCentralWidget(main_frame)
30-
31-
def onButtonClick(self ):
32-
print('The button1 被按下了' )
33-
34-
35-
if __name__ == "__main__":
36-
app = QApplication(sys.argv)
37-
form = WinForm()
38-
form.show()
39-
sys.exit(app.exec_())
1+
# -*- coding: utf-8 -*-
2+
"""
3+
【简介】
4+
信号和槽例子
5+
6+
7+
"""
8+
9+
from PyQt5.QtWidgets import QPushButton , QApplication, QWidget
10+
from PyQt5.QtWidgets import QMessageBox
11+
import sys
12+
13+
app = QApplication(sys.argv)
14+
widget = QWidget()
15+
16+
def showMsg():
17+
QMessageBox.information(widget, "信息提示框", "ok,弹出测试信息")
18+
btn = QPushButton( "测试点击按钮", widget)
19+
btn.clicked.connect( showMsg)
20+
widget.show()
21+
sys.exit(app.exec_())

‎Chapter07/qt07_winSignalSlot02.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
88
"""
99

10-
from PyQt5.QtCore import pyqtSignal
11-
from PyQt5.QtWidgets import QMainWindow,QHBoxLayout, QPushButton , QApplication, QWidget , QMessageBox
10+
11+
from PyQt5.QtWidgets import QMainWindow,QHBoxLayout, QPushButton , QApplication, QWidget
1212
import sys
1313

1414
class WinForm(QMainWindow):
15-
btnlickedSignal = pyqtSignal(int)
16-
15+
1716
def __init__(self, parent=None):
1817
super(WinForm, self).__init__(parent)
1918
self.setWindowTitle('部件中的信号槽通信')
20-
# 声明自定义的信号
21-
self.btnlickedSignal.connect(self.getSignal)
19+
2220
self.button1 = QPushButton('Button 1')
2321
#
2422
self.button1.clicked.connect(self.onButtonClick)
@@ -31,12 +29,7 @@ def __init__(self, parent=None):
3129
self.setCentralWidget(main_frame)
3230

3331
def onButtonClick(self ):
34-
print('The button1 被按下了' )
35-
self.btnlickedSignal.emit(10)
36-
37-
def getSignal(self, intVal ):
38-
QMessageBox.information(self, "信息提示框", '收到信号传过来的值:' + str(intVal) )
39-
32+
print('The button1 被按下了' )
4033

4134

4235
if __name__ == "__main__":

‎Chapter07/qt07_winSignalSlot03.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
"""
44
【简介】
5-
部件中的信号槽传递额外参数示例
5+
部件中的信号槽通信示例
66
77
88
"""
99

10-
from PyQt5.QtCore import *
11-
from PyQt5.QtGui import *
12-
from PyQt5.QtWidgets import *
10+
from PyQt5.QtCore import pyqtSignal
11+
from PyQt5.QtWidgets import QMainWindow,QHBoxLayout, QPushButton , QApplication, QWidget , QMessageBox
1312
import sys
14-
from functools import partial
1513

1614
class WinForm(QMainWindow):
15+
btnlickedSignal = pyqtSignal(int)
16+
1717
def __init__(self, parent=None):
18-
super(WinForm, self).__init__(parent)
19-
button1 = QPushButton('Button 1')
20-
button2 = QPushButton('Button 2')
18+
super(WinForm, self).__init__(parent)
19+
self.setWindowTitle('部件中的信号槽通信')
20+
# 声明自定义的信号
21+
self.btnlickedSignal.connect(self.getSignal)
22+
self.button1 = QPushButton('Button 1')
23+
# 使用信号连接槽函数,槽函数不用加括号
24+
self.button1.clicked.connect(self.onButtonClick )
2125

22-
button1.clicked.connect(lambda: self.onButtonClick(1))
23-
24-
button2.clicked.connect(partial(self.onButtonClick, 2))
25-
2626
layout = QHBoxLayout()
27-
layout.addWidget(button1)
28-
layout.addWidget(button2)
29-
27+
layout.addWidget(self.button1)
3028
main_frame = QWidget()
31-
main_frame.setLayout(layout)
29+
main_frame.setLayout(layout)
3230
self.setCentralWidget(main_frame)
3331

34-
def onButtonClick(self, n):
35-
print('Button {0} 被按下了'.format(n))
36-
QMessageBox.information(self, "信息提示框", 'Button {0} clicked'.format(n))
37-
38-
32+
def onButtonClick(self ):
33+
print('The button1 被按下了' )
34+
self.btnlickedSignal.emit(10)
35+
36+
def getSignal(self, intVal ):
37+
QMessageBox.information(self, "信息提示框", '收到信号传过来的值:' + str(intVal) )
38+
3939
if __name__ == "__main__":
4040
app = QApplication(sys.argv)
4141
form = WinForm()

‎Chapter07/qt07_winSignalSlot04.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
【简介】
5+
部件中的信号槽传递额外参数示例
6+
7+
8+
"""
9+
10+
from PyQt5.QtCore import *
11+
from PyQt5.QtGui import *
12+
from PyQt5.QtWidgets import *
13+
import sys
14+
from functools import partial
15+
16+
class WinForm(QMainWindow):
17+
def __init__(self, parent=None):
18+
super(WinForm, self).__init__(parent)
19+
button1 = QPushButton('Button 1')
20+
button2 = QPushButton('Button 2')
21+
22+
button1.clicked.connect(lambda: self.onButtonClick(1))
23+
24+
button2.clicked.connect(partial(self.onButtonClick, 2))
25+
26+
layout = QHBoxLayout()
27+
layout.addWidget(button1)
28+
layout.addWidget(button2)
29+
30+
main_frame = QWidget()
31+
main_frame.setLayout(layout)
32+
self.setCentralWidget(main_frame)
33+
34+
def onButtonClick(self, n):
35+
print('Button {0} 被按下了'.format(n))
36+
QMessageBox.information(self, "信息提示框", 'Button {0} clicked'.format(n))
37+
38+
39+
if __name__ == "__main__":
40+
app = QApplication(sys.argv)
41+
form = WinForm()
42+
form.show()
43+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)
Please sign in to comment.