PyQt实践教学(三)–Events and signals in PyQt5
admin
2023-07-30 20:56:38
0

终于到了QT中最重要的部分了,就跟前端页面如果只有div+css的而少了JS会变得死气沉沉一样,事件驱动才是才是QT中的核心,它将静态的各个widget相互关联响应起来使得整个GUI变得生机盎然起来.
废话少说了,上实例~

Events

All GUI applications are event-driven. Events are generated mainly by the user of an application. But they can be generated by other means as well: e.g. an Internet connection, a window manager, or a timer. When we call the application\’s exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects.
In the event model, there are three participants:

  • event source
  • event object
  • event target

The event source is the object whose state changes. It generates events. The event object (event) encapsulates the state changes in the event source. The event target is the object that wants to be notified. Event source object delegates the task of handling an event to the event target.
PyQt5 has a unique signal and slot mechanism to deal with events. Signals and slots are used for communication between objects. A signal is emitted when a particular event occurs. A slot can be any Python callable. A slot is called when its connected signal is emitted.

Signals & slots(事件与信号)

# coding=utf-8
\"\"\"信号槽示例\"\"\"
import sys
from PyQt5.QtWidgets import QWidget,QLCDNumber,QSlider,QVBoxLayout,QApplication
from PyQt5.QtCore import Qt

class SignalSlot(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        lcd = QLCDNumber(self)
        slider = QSlider(Qt.Horizontal, self)

        v_box = QVBoxLayout()
        v_box.addWidget(lcd)
        v_box.addWidget(slider)

        self.setLayout(v_box)
        slider.valueChanged.connect(lcd.display)

        self.setGeometry(300,300,250,150)
        self.setWindowTitle(\"信号槽演示程序\")
        self.show()

if __name__ == \'__main__\':
    app = QApplication(sys.argv)
    qb = SignalSlot()
    sys.exit(app.exec_())
  1. Here we connect a valueChanged signal of the slider to the display slot of the lcd number
  2. The sender is an object that sends a signal. The receiver is the object that receives the signal. The slot is the method that reacts to the signal.

Reimplementing event handler(重写事件的处理方法)

# coding=utf-8
\"\"\"用Esc键推出示例\"\"\"
import sys
from PyQt5.QtWidgets import QWidget,QApplication
from PyQt5.QtCore import Qt

class Escape(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.resize(250, 150)
        self.setWindowTitle(\"Esc退出演示程序\")
        self.show()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.close()

if __name__ == \'__main__\':
    app = QApplication(sys.argv)
    escape = Escape()
    sys.exit(app.exec_())
  1. In our example, we reimplement the keyPressEvent() event handler.
  2. If we click the Escape button, the application terminates.

Event sender (事件发送者)

#!/usr/bin/python3
# coding=utf-8
\"\"\"发射信号示例\"\"\"
import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton,QApplication

class EmitSignal(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        btn1 = QPushButton(\"Button 1\",self)
        btn1.move(30,50)
        btn2 = QPushButton(\"Button 2\", self)
        btn2.move(150, 50)

        btn1.clicked.connect(self.buttonClicked)
        btn2.clicked.connect(self.buttonClicked)
        self.statusBar()

        self.setGeometry(300,300,290,150)
        self.setWindowTitle(\"发射信号演示程序\")
        self.show()

    def buttonClicked(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text()+\"was pressed\")

if __name__ == \'__main__\':
    app = QApplication(sys.argv)
    es = EmitSignal()
    sys.exit(app.exec_())
  1. We have two buttons in our example. In the buttonClicked() method we determine which button we have clicked by calling the sender() method.
  2. Both buttons are connected to the same slot.
  3. We determine the signal source by calling the sender() method. In the statusbar of the application, we show the label of the button being pressed.

emitting signals (发射信号)

# coding=utf-8
\"\"\"发射信号示例\"\"\"
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QWidget
from PyQt5.QtCore import pyqtSignal,QObject

class Communication(QObject):
    closeApp = pyqtSignal()

class EmitSignal(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.c = Communication()
        self.c.closeApp.connect(self.close)

        self.resize(250, 150)
        self.setWindowTitle(\"发射信号演示程序\") 
        self.show()

    def mousePressEvent(self, QMouseEvent):
        self.c.closeApp.emit()

if __name__ == \'__main__\':
    app = QApplication(sys.argv)
    es = EmitSignal()
    sys.exit(app.exec_())
  1. We create a new signal called closeApp. This signal is emitted during a mouse press event. The signal is connected to the close() slot of the QMainWindow.
  2. A signal is created with the pyqtSignal() as a class attribute of the external Communicate class.
  3. The custom closeApp signal is connected to the close() slot of the QMainWindow.
  4. When we click on the window with a mouse pointer, the closeApp signal is emitted. The application terminates.

In this part of the PyQt5 tutorial, we have covered signals and slots.

总结:

  1. 当我们想要触发一个事件的时候,我们可以用某个信号的emit()方法发射此信号,此时会执行绑定在这个事件(即connect()的参数)上的方法.正如按钮的clicked属性一样,它也是一个信号,正如前面我在《PyQt实践教学(一)》中那个Closing a window那一节例子一样,clicked.connect()就是给这个信号绑定一个事件。这就是信号槽机制
  2. 一般都是Qwidgets中的某个组件中的某个表示变化的属性(非方法),例如clicked,valuChanged等,再接上connect(响应函数)来处理动态效果
  3. 被触发的组件叫做sender,而后触发组件再继续触发的动作或者函数叫slot

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...