QPushButton 控件的 pressed
信號和 released
信號分別是在按鈕被 點擊
和 釋放
的瞬間發出,以此來實現多個信號連接同一個槽函數:
"""
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__()
self.label = QLabel("I am a Label", self)
self.button = QPushButton("Button", self)
self.button.pressed.connect(self.change_label)
self.button.released.connect(self.change_label)
self.v_layout = QVBoxLayout()
self.v_layout.addWidget(self.label)
self.v_layout.addWidget(self.button)
self.setLayout(self.v_layout)
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):
>>> flag
When released:
>>> flag
>>> flag