Home  >  Article  >  Backend Development  >  Events and signals that you must learn every day in PyQt5

Events and signals that you must learn every day in PyQt5

不言
不言Original
2018-04-20 13:59:453254browse

This article mainly introduces in detail the relevant information about events and signals that must be learned every day in PyQt5. It has a certain reference value. Interested friends can refer to it.

We will explore this part How PyQt5 events and signals are implemented in applications.

EventsEvents

All GUI applications are event-driven. Application events are primarily generated by the user, but they can also be generated by other methods, such as 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 detects various events and sends them to event objects.

In the event model, there are three participants:

  • event source (event source)

  • event object (event Object)

  • event target (event target)

The event source is the state change of the object that generates events. An event object (event) is an object that encapsulates state changes in an event source. The event target is the object that wishes to be notified. The event source object represents the task of processing an event to the event target.

PyQt5 uses a unique signal and slot mechanism to handle events. Signals and slots are used for communication between objects. When a specific event occurs, a signal is emitted. The slot can be any Python call. The signal is emitted when the slot connected to it is called.

Signals & slotsSignals and slots

This is a simple example demonstrating PyQt5's signals and slots.


#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

这个例子中,我们将QSlider的滑动信号连接到QLCDNumber中。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月1日
"""

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QSlider, 
QLCDNumber, QVBoxLayout)
from PyQt5.QtCore import Qt

class Example(QWidget):

 def __init__(self):
 super().__init__()

 self.initUI()

 def initUI(self):

 lcd = QLCDNumber(self)
 sld = QSlider(Qt.Horizontal, self)

 vbox = QVBoxLayout()
 vbox.addWidget(lcd)
 vbox.addWidget(sld)

 self.setLayout(vbox)
 sld.valueChanged.connect(lcd.display)

 self.setGeometry(300, 300, 250, 150)
 self.setWindowTitle('信号/槽') 
 self.show()

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())


In our example, QtGui.QLCDNumber and QtGui.QSlider will be used. We change the LCD numbers by dragging the slider.


sld.valueChanged.connect(lcd.display)


Here, the valueChanged signal of the slider is connected to the display slot of the lcd.

A transmitter is an object that sends signals. A receiver is an object that receives a signal. The slot is the method of feedback to the signal.

After the program is executed

Events and signals that you must learn every day in PyQt5

Override the system event handler

Events are often processed in PyQt5 through Override the event handler.


#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在这个例子中,我们执行事件处理程序。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月1日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt

class Example(QWidget):

 def __init__(self):
 super().__init__()

 self.initUI()

 def initUI(self):

 self.setGeometry(300, 300, 250, 150)
 self.setWindowTitle('事件处理') 
 self.show()

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

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())


In our case, we reimplement the keyPressEvent() event handler.


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


If we press the Esc key on the keyboard, the application terminates.

Event senderEvent sending

In order to easily distinguish multiple event sources connected to the same event target, the sender() method can be used in PyQt5.


#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在这个例子中,我们确定事件发送对象。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月1日
"""

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton

class Example(QMainWindow):

 def __init__(self):
 super().__init__()

 self.initUI()

 def initUI(self):

 btn1 = QPushButton('按钮一', self)
 btn1.move(30, 50)

 btn2 = QPushButton('按钮二', self)
 btn2.move(150, 50)

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

 self.statusBar()

 self.setGeometry(300, 300, 300, 150)
 self.setWindowTitle('事件发送') 
 self.show()

 def buttonClicked(self):

 sender = self.sender()
 self.statusBar().showMessage(sender.text() + ' 被按下')

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())


In our example there are two buttons. Both buttons are connected to the buttonClicked() method and we respond to the clicked button by calling the sender() method.


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


Two buttons are connected to the same slot.


def buttonClicked(self):

 sender = self.sender()
 self.statusBar().showMessage(sender.text() + ' 被按下')


We determine the signal source by calling the sender() method. In the application's status bar, displays the label of the pressed button.

After the program is executed

Events and signals that you must learn every day in PyQt5

Customized emission signal

Signals can be emitted from an object created by a QObject. In the following example we will look at how we can customize the emitted signal.


#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在这个例子中,我们显示了如何以发射信号。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月1日
"""

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import pyqtSignal, QObject

class Communicate(QObject):
 closeApp = pyqtSignal()

class Example(QMainWindow):

 def __init__(self):
 super().__init__()

 self.initUI()

 def initUI(self):

 self.c = Communicate()
 self.c.closeApp.connect(self.close)

 self.setGeometry(300, 300, 300, 150)
 self.setWindowTitle('发射信号') 
 self.show()

 def mousePressEvent(self, event):

 self.c.closeApp.emit()

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())


We create a new signal called closeApp. This signal emits a mouse press event. This signal is connected to the close() slot in QMainWindow.


class Communicate(QObject):
 closeApp = pyqtSignal()


Create a Communicate class inherited from QObject, which has a property of the pyqtSignal() class.


self.c = Communicate()
self.c.closeApp.connect(self.close)


Connect our custom closeApp signal to the close() slot in QMainWindow.


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


When our mouse clicks on the program window, the closeApp signal is emitted (emit). Application terminated.

Related recommendations:

Python PyQt4 implements QQ drawer effect

PyQt implements interface flip switching effect


The above is the detailed content of Events and signals that you must learn every day in PyQt5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn