PyQt5 & PySide2信号与槽机制1
2024-09-08 19:02:49
pyside2&pyqt5的信号与槽机制
1、信号与槽的两种写法
第一种情况:
from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
btn.clicked.connect(widget.close)
widget.show()
sys.exit(app.exec_())
第二种情况:
from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
QtCore.QObject.connect(btn,QtCore.SIGNAL("clicked()"),widget,QtCore.SLOT("close()"))
widget.show()
sys.exit(app.exec_())
2、自定义槽函数
from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
@QtCore.Slot()
def changeTex():
btn.setText("ok")
btn.clicked.connect(changeTex)
widget.show()
sys.exit(app.exec_())
3、带参数槽函数
(1)lambad写法:
from PySide2 import QtWidgets,QtCore
import sys
class MyForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QtWidgets.QPushButton(self)
button1.clicked.connect(lambda: self.on_button(1))
def on_button(self, n):
print('Button {0} clicked'.format(n))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
(2)使用functools里的partial函数。
from functools import partial
from PySide2 import QtWidgets,QtCore
import sys
class MyForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QtWidgets.QPushButton(self)
button1.clicked.connect(partial(self.on_button, 1))
def on_button(self, n):
print('Button {0} clicked'.format(n))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
最新文章
- local认证
- 内存对齐 和 sizeof小结
- xenomai for at91
- nodejs入门
- 利用注解进行sql反射代码示例
- 69. Letter Combinations of a Phone Number
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
- Asynchronous
- poj 1573Robot Motion
- 二 Djano模型层之模型字段选项
- Jmeter 性能测试术语
- Android jni c/c++线程通过CallVoidMethod调用java函数出现奔溃问题
- MogileFS-2.44 安装与配置
- Material Design Support 8大控件介绍
- 找不到reportviewer控件在哪儿
- 转码:gcc在代码中禁止某些warning
- Linux调度器 - deadline调度器
- 如何理解Latency和Throughput: 吞吐量和延迟
- this指向 - Node环境
- 【Qt开发】QTime类
热门文章
- drush use dev.mentor.com | expecting statement
- clickhouse杂记
- wsl无法创建文件与修改文件
- ubuntu 16.04 安装peach
- Mac下Virtual Box 6.1 Host-Only 网络配置 没有虚拟网卡
- hierarchical-clustering
- Bug_Android error: duplicate attribute. (错误:重复属性。)
- SQLSERVER 根据一个库的视图在另一个库中生成一张表
- js实现不同的域名 输出不同的结果
- 【SQL Server】numeric——精确数字的数据类型