Home Backend Development Python Tutorial Events and signals that you must learn every day in PyQt5

Events and signals that you must learn every day in PyQt5

Apr 20, 2018 pm 01:59 PM
pyqt5 event Signal

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.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

#!/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_())

Copy after login


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


1

sld.valueChanged.connect(lcd.display)

Copy after login


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.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#!/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_())

Copy after login


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


1

2

3

def keyPressEvent(self, e):

 if e.key() == Qt.Key_Escape:

 self.close()

Copy after login


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.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

#!/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_())

Copy after login


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.


1

2

btn1.clicked.connect(self.buttonClicked)

btn2.clicked.connect(self.buttonClicked)

Copy after login


Two buttons are connected to the same slot.


1

2

3

4

def buttonClicked(self):

 

 sender = self.sender()

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

Copy after login


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.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

#!/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_())

Copy after login


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


1

2

class Communicate(QObject):

 closeApp = pyqtSignal()

Copy after login


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


1

2

self.c = Communicate()

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

Copy after login


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


1

2

def mousePressEvent(self, event):

 self.c.closeApp.emit()

Copy after login


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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install pyqt5 How to install pyqt5 Nov 30, 2023 pm 02:05 PM

How to install pyqt5

Event ID 4660: Object deleted [Fix] Event ID 4660: Object deleted [Fix] Jul 03, 2023 am 08:13 AM

Event ID 4660: Object deleted [Fix]

What is an analog signal and what is a digital signal What is an analog signal and what is a digital signal Jan 30, 2023 pm 02:44 PM

What is an analog signal and what is a digital signal

Get upcoming calendar events on your iPhone lock screen Get upcoming calendar events on your iPhone lock screen Dec 01, 2023 pm 02:21 PM

Get upcoming calendar events on your iPhone lock screen

Super complete! Python graphical interface framework PyQt5 usage guide! Super complete! Python graphical interface framework PyQt5 usage guide! Apr 13, 2023 am 08:43 AM

Super complete! Python graphical interface framework PyQt5 usage guide!

Solution to the problem that the mobile phone cannot connect to WiFi (how to solve the problem that the mobile phone has a WiFi signal but cannot access the Internet) Solution to the problem that the mobile phone cannot connect to WiFi (how to solve the problem that the mobile phone has a WiFi signal but cannot access the Internet) Apr 19, 2024 pm 03:25 PM

Solution to the problem that the mobile phone cannot connect to WiFi (how to solve the problem that the mobile phone has a WiFi signal but cannot access the Internet)

In JavaScript, what is the purpose of the 'oninput' event? In JavaScript, what is the purpose of the 'oninput' event? Aug 26, 2023 pm 03:17 PM

In JavaScript, what is the purpose of the 'oninput' event?

How to implement calendar functions and event reminders in PHP projects? How to implement calendar functions and event reminders in PHP projects? Nov 02, 2023 pm 12:48 PM

How to implement calendar functions and event reminders in PHP projects?

See all articles