PyQt信号槽用法详解介绍与代码演示

一  信号与槽的绑定

在窗口上加一个按钮,并绑定信号槽

        # 添加Button
        self.button1 = QPushButton('AAA')

        # 将信号与槽关联
        self.button1.clicked.connect(self.onClick_Button)

 这与C++的写法不同
     connect(ui.button1, SIGNAL(clicked()),this,SLOT(onClick_Button));

槽函数的定义

def onClick_Button(self):
        #获取触发的对象
        sender = self.sender()
        QMessageBox.information(self, '标题', sender.text() + '按钮被点击了')

运行结果如下:

PyQt信号槽用法详解介绍与代码演示

本例子全部代码:

import sys

#导入代码需要的模块,就像C++需要添加头文件
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QHBoxLayout,QPushButton,QMessageBox
from PyQt5.QtGui import QIcon

class FirstMainWin(QMainWindow):

    #构造函数初始化Window
    def __init__(self):
        super(FirstMainWin,self).__init__()

        # 设置主窗口的标题
        self.setWindowTitle('第一个纯Python代码写的主窗口应用')

        # 设置窗口的尺寸
        self.resize(400,300)

        self.status = self.statusBar()

        self.status.showMessage('我是状态栏消息,5秒后消失',5000)

        # 添加Button
        self.button1 = QPushButton('AAA')

        # 将信号与槽关联
        self.button1.clicked.connect(self.onClick_Button)

        layout = QHBoxLayout()
        layout.addWidget(self.button1)

        mainFrame = QWidget()
        mainFrame.setLayout(layout)

        self.setCentralWidget(mainFrame)

    # 按钮槽函数
    def onClick_Button(self):
        #获取触发的对象
        sender = self.sender()
        QMessageBox.information(self, '标题', sender.text() + '按钮被点击了')

if __name__ == '__main__':
    app = QApplication(sys.argv)

    app.setWindowIcon(QIcon('JXC.ico'))
    main = FirstMainWin()
    main.show()

    sys.exit(app.exec_())

 

二 信号槽自动连接

     除了上面的连接方式,pyqt提供了类似java注解形势的信号槽自动连接。例如下面的形势:

@QtCore.pyqtSlot()
def on_okButton_clicked(self):
    print("点击了ok按钮")

@QtCore.pyqtSlot()
def on_cancelButton_clicked(self):
    print("点击了cancel按钮")

     如果槽函数写成on_控件名字_clicked的形势,在C++中是默认进行连接的,python里不行。还需要通过元对象信息进行设置。本例子的界面如下:

PyQt信号槽用法详解介绍与代码演示

    点击这ok和cancel会分别打印不同的信息

PyQt信号槽用法详解介绍与代码演示

、代码如下:

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton
import sys

class AutoSignalSlot(QWidget):
    def __init__(self):
        super(AutoSignalSlot,self).__init__()
        self.resize(300,100)
        self.okButton = QPushButton("ok",self)
        self.okButton.setObjectName("okButton")
        self.okButton1 = QPushButton("cancel",self)
        self.okButton1.setObjectName("cancelButton")
        layout = QHBoxLayout()
        layout.addWidget(self.okButton)
        layout.addWidget(self.okButton1)
        self.setLayout(layout)
        QtCore.QMetaObject.connectSlotsByName(self)

    @QtCore.pyqtSlot()
    def on_okButton_clicked(self):
        print("点击了ok按钮")

    @QtCore.pyqtSlot()
    def on_cancelButton_clicked(self):
        print("点击了cancel按钮")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = AutoSignalSlot()
    example.show()
    sys.exit(app.exec_())

    通过设置控件元对象名字,再绑定槽,其实这种反而复杂了

self.okButton = QPushButton("ok",self)
self.okButton.setObjectName("okButton")
QtCore.QMetaObject.connectSlotsByName(self)

三 lambda函数写信号槽

    pyqt使用lambda写信号槽,用法更灵活,可以给槽函数传不同参数,demo代码如下

from PyQt5.QtWidgets import *
import sys

class LambdaSlotArg(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(300,100)

        button1 = QPushButton("按钮1")
        button2 = QPushButton("按钮2")
        ok = 100
        button1.clicked.connect(lambda : self.onButtonClick(10,ok))
        button2.clicked.connect(lambda : self.onButtonClick(ok,-20))
        button1.clicked.connect(lambda : QMessageBox.information(self,"结果","单击了button1"))

        layout = QHBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

    def onButtonClick(self,m,n):
        print("m + n =",m + n )
        QMessageBox.information(self,"结果",str(m+n))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = LambdaSlotArg()
    form.show()
    sys.exit(app.exec_())

四 partial参数化

  当有多个按钮公用一个槽函数,只需要改变某些参数即可实现不同功能,此时可以用partial关键字实现,代码如下:

from PyQt5.QtWidgets import *
import sys
from functools import partial

class PartialSlotArg(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Partial用法")

        button1 = QPushButton("按钮1")
        button2 = QPushButton("按钮2")
        x = 20
        y = -123
        button1.clicked.connect(partial(self.onButtonClick,10,20))
        button2.clicked.connect(partial(self.onButtonClick, x, y))

        layout = QHBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

    def onButtonClick(self,m,n):
        print("m + n =",m + n )
        QMessageBox.information(self,"结果",str(m+n))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = PartialSlotArg()
    form.show()
    sys.exit(app.exec_())

 

上一篇:JavaScript对象基础


下一篇:pyqt QLabel详细用法