「PyQt5」2. QPushButton 的 signals 和 slots
简述:Graphical Application
是事件驱动型的,和控制台或终端程序不同。所谓事件,就是诸如点击按钮、选择多选框中一条选项这样的用户行为。本文记录了QPushButton
的signals
和slots
用法。
1 名词解释
signal
翻译为信号,指事件发生时PyQt5 widget
发送的信号。slot
:翻译为槽(奇奇怪怪),是任何可调用的函数或方法。
对于QPushButton
来说,signal
是点击按钮时QPushButton
发送的Clicked()
信号,slot
可以是自己编写的可调用函数/方法,也可以是PyQt5
自有的函数/方法。
2 QPushButton的signals
QPushButton
具有如下signal:
clicked()
:This signal is emitted when the button is activated (i.e., pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().pressed()
:This signal is emitted when the button is pressed down.released()
:This signal is emitted when the button is released.toggled()
:This signal is emitted whenever a checkable button changes its state.destroyed()
:类被摧毁,信号不可阻断。objectNameChanged()
:类名变化。
前四者继承自PyQt5.QtWidgets.QWidget.QAbstractButton
,后二者继承自PyQt5.QtCore.QObject
。
3 实例
1 | # coding=utf-8 |
self.btn.clicked.connect(self.prt)
将clicked()
与处理程序prt()
链接起来,其中,clicked()
是signal
,prt()
是slot
。除此之外,还有另外一种链接办法:
1 | PyQt5.QtCore.QObject.connect(widget, QtCore.SIGNAL(‘signalname’), slot_function) |
**以上!**
「PyQt5」2. QPushButton 的 signals 和 slots
https://alexinst.github.io/Python/PyQt5/signals-and-slots-of-button/