【PyQt5】{9} —— 多个信号连接同一个槽函数

QPushButton 控件的 pressed 信号和 released 信号分别是在按钮被 点击释放 的瞬间发出,以此来实现多个信号连接同一个槽函数:
# -*- coding: utf-8 -*-
"""
Created on Sat May  9 11:11:21 2020

@author: Giyn
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout


class Simple_Window(QWidget):
    def __init__(self):
        super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
        self.label = QLabel("I am a Label", self) # self是指定的父类Simple_Window,表示QLabel属于Simple_Window窗口
        self.button = QPushButton("Button", self)
        
        # 连接信号和槽
        self.button.pressed.connect(self.change_label)
        self.button.released.connect(self.change_label)
        
        self.v_layout = QVBoxLayout() # 实例化一个QVBoxLayout对象
        self.v_layout.addWidget(self.label)
        self.v_layout.addWidget(self.button)
        
        self.setLayout(self.v_layout) # 调用窗口的setLayout方法将总布局设置为窗口的整体布局
        
    def change_label(self):
        self.label.setText("I am changed")
        print("flag")
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Simple_Window()
    window.show()
    sys.exit(app.exec())

Output:

When push (not released):

【PyQt5】{9} —— 多个信号连接同一个槽函数_第1张图片

>>> flag
When released:

【PyQt5】{9} —— 多个信号连接同一个槽函数_第2张图片

>>> flag
>>> flag

你可能感兴趣的:(【PyQt5】)