PyQt5学习笔记16----PyQt信号和槽传递额外参数
2015-04-24 15:00
204 查看
使用Pyqt编程过程中,经常会遇到给槽函数传递额外参数的情况。但是信号-槽机制只是指定信号如何连接到槽,信号定义的参数被传递给槽,而额外的参数(用户定义)不能直接传递。
而传递额外参数又是很有用处。你可能使用一个槽处理多个组件的信号,有时要传递额外的信息。
一种方法是使用lambda表达式。
[python] view
plaincopy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 1')
button1.clicked.connect(lambda: self.on_button(1))
button2.clicked.connect(lambda: self.on_button(2))
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
def on_button(self, n):
print('Button {0} clicked'.format(n))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
解释一下,on_button是怎样处理从两个按钮传来的信号。我们使用lambda传递按钮数字给槽,也可以传递任何其他东西---甚至是按钮组件本身(假如,槽打算把传递信号的按钮修改为不可用)
第2个方法是使用functools里的partial函数。
[python] view
plaincopy
button1.clicked.connect(partial(self.on_button, 1))
button2.clicked.connect(partial(self.on_button, 2))
哪个办法好一点?这个属于风格的问题。个人观点,喜欢lambda,条理清楚,而且灵活。
《Rapid GUI Program with Python and QT》 P143例子。
[python] view
plaincopy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from functools import partial
import sys
class Bu1(QWidget):
def __init__(self, parent=None):
super(Bu1, self).__init__(parent)
#水平盒式布局
layout = QHBoxLayout()
#显示
self.lbl = QLabel('no button is pressed')
#循环5个按钮
for i in range(5):
but = QPushButton(str(i))
layout.addWidget(but)
#信号和槽连接
but.clicked.connect(self.cliked)
#使用封装,lambda
but = QPushButton('5')
layout.addWidget(but)
but.clicked.connect(lambda: self.on_click('5'))
#使用个who变量,结果不正常,显示 False is pressed
#but.clicked.connect(lambda who="5": self.on_click(who))
#使用封装,partial函数
but = QPushButton('6')
layout.addWidget(but)
but.clicked.connect(partial(self.on_click, '6'))
layout.addWidget(self.lbl)
#设置布局
self.setLayout(layout)
#传递额外参数
def cliked(self):
bu = self.sender()
if isinstance(bu, QPushButton):
self.lbl.setText('%s is pressed' % bu.text())
else:
self.lbl.setText('no effect')
def on_click(self, n):
self.lbl.setText('%s is pressed' % n)
if __name__ == '__main__':
app = QApplication(sys.argv)
bu =Bu1()
bu.show()
app.exec_()
而传递额外参数又是很有用处。你可能使用一个槽处理多个组件的信号,有时要传递额外的信息。
一种方法是使用lambda表达式。
[python] view
plaincopy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 1')
button1.clicked.connect(lambda: self.on_button(1))
button2.clicked.connect(lambda: self.on_button(2))
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
def on_button(self, n):
print('Button {0} clicked'.format(n))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
解释一下,on_button是怎样处理从两个按钮传来的信号。我们使用lambda传递按钮数字给槽,也可以传递任何其他东西---甚至是按钮组件本身(假如,槽打算把传递信号的按钮修改为不可用)
第2个方法是使用functools里的partial函数。
[python] view
plaincopy
button1.clicked.connect(partial(self.on_button, 1))
button2.clicked.connect(partial(self.on_button, 2))
哪个办法好一点?这个属于风格的问题。个人观点,喜欢lambda,条理清楚,而且灵活。
《Rapid GUI Program with Python and QT》 P143例子。
[python] view
plaincopy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from functools import partial
import sys
class Bu1(QWidget):
def __init__(self, parent=None):
super(Bu1, self).__init__(parent)
#水平盒式布局
layout = QHBoxLayout()
#显示
self.lbl = QLabel('no button is pressed')
#循环5个按钮
for i in range(5):
but = QPushButton(str(i))
layout.addWidget(but)
#信号和槽连接
but.clicked.connect(self.cliked)
#使用封装,lambda
but = QPushButton('5')
layout.addWidget(but)
but.clicked.connect(lambda: self.on_click('5'))
#使用个who变量,结果不正常,显示 False is pressed
#but.clicked.connect(lambda who="5": self.on_click(who))
#使用封装,partial函数
but = QPushButton('6')
layout.addWidget(but)
but.clicked.connect(partial(self.on_click, '6'))
layout.addWidget(self.lbl)
#设置布局
self.setLayout(layout)
#传递额外参数
def cliked(self):
bu = self.sender()
if isinstance(bu, QPushButton):
self.lbl.setText('%s is pressed' % bu.text())
else:
self.lbl.setText('no effect')
def on_click(self, n):
self.lbl.setText('%s is pressed' % n)
if __name__ == '__main__':
app = QApplication(sys.argv)
bu =Bu1()
bu.show()
app.exec_()
相关文章推荐
- pyqt信号和槽传递额外参数
- pyqt信号和槽传递额外参数
- pyqt中信号与槽的参数传递。
- Python学习笔记7-把函数当参数传递、指定可变参数
- C++ Primer 学习笔记_21_函数 --函数定义、参数传递
- SpringMVC学习笔记(6) 参数的传递+乱码解决
- 【菜鸟C++学习笔记】22. 参数的传递机制
- java学习笔记3--参数的传递方式
- java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释
- C#学习笔记一--C#中的参数传递
- C#学习笔记:参数传递(详解)
- python学习笔记-(13)python中的函数参数传递
- Java学习笔记2016.2.17 方法参数传递
- Struts2学习笔记1----Struts2框架action与view之间的参数传递小测试!
- pyqt4信号与槽之间参数传递
- python学习笔记11(函数二): 参数的传递、变量的作用域
- Python学习笔记7-把函数当参数传递、指定可变参数
- JSP学习笔记(六)-----在多个JSP页面之间传递参数
- spring MVC学习笔记(三) spring MVC视图与controller之间参数传递
- air 学习笔记:bat 调用air 传递参数 打包