说明:文章主要演示在PyQt5中如何对Button进行事件绑定,以及执行相应的操作。
演示代码复制可用。
import sys
from PyQt5 import QtWidgets
class ClassPushButtonClicked(QtWidgets.QWidget):
def __init__(self):
super(ClassPushButtonClicked, self).__init__()
self.init_ui()
self.bind_trigger()
def init_ui(self):
"""Init default ui"""
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle("PushButtonClicked")
button = QtWidgets.QPushButton(text="pushButton", parent=self)
button.setToolTip("Test pushButton!")
button_default_size = button.sizeHint()
button.setGeometry(100, 100, 100, 35)
self.button = button
label = QtWidgets.QLabel(text="label", parent=self)
label.setText("Button default size: " + str(button_default_size))
label.setGeometry(10, 180, 200, 35)
self.label = label
def bind_trigger(self):
# Button clicked trigger
self.button.clicked.connect(self.clicked_button)
def clicked_button(self):
self.label.setText("<b style='color:red'>Button has been clicked!</b>")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
push_button_clicked = ClassPushButtonClicked()
push_button_clicked.show()
sys.exit(app.exec_())