In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "Example Analysis of Events and Signals in PyQt5". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn the article "Example Analysis of Events and Signals in PyQt5".
Events event
All GUI applications are event driven. Application events are generated primarily by users, 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 events and sends them to event objects.
In the event model, there are three participants:
event source
event object
event target
An event source is an event caused by a change in the state of an object. An event object (event) is an object whose state changes encapsulated in an event source. The event target is the object you want to be notified of. The event source object represents the task of processing an event to the event target.
PyQt5 uses unique signal and slot mechanisms to process events. Signals and slots are used for communication between objects, and signals are emitted when a particular event occurs. A slot can be any Python call. Signal transmission when connected to slot is invoked.
Signals & slots Signals & slots
This is a simple example demonstrating PyQt5 signals and slots.
#!/ usr/bin/python3# -*- coding: utf-8-*-""PyQt5 tutorial In this example, we concatenate the sliding signal of QSlider into QLCDNumber. Author: My World You've been to my blog: http://blog.csdn.net/weiaitaowang Last edited: August 1, 2016 ""import sysfrom PyQt5.QtWidgets import (QApplication, QWidget, QSlider, QLCDNumber, QVBoxLayout)from PyQt5.QtCore import Qtclass 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 ('Signal/Slot') self.show ()if __name__ =='__main_': app = QApplication(sys.argv) ex = Example() sys. example (app.exec_())
In our example, we will use QtGui.QLCDNumber and QtGui.QSlider. We change LCD numbers by dragging the slider.
sld.valueChanged.connect(lcd.display)
Here, the slider's valueChanged signal is connected to the LCD's display slot.
The transmitter is the object sending the signal. A receiver is an object that receives a signal. Slot is the way to feed back signals.
After the program is executed,
overwrite system event handler
Event handling in PyQt5 tends to handle programs by rewriting events.
#!/ /usr/bin/python3# -*- coding: utf-8-*-""PyQt5 tutorial In this example, we execute an event handler. Author: My World You have been to blog: http://blog.csdn.net/weiaitaowang Last edited: August 1, 2016 ""import sysfrom PyQt5.QtWidgets import QApplication, QWidgetfrom PyQt5.QtCore import Qtclass Example(QWidget): def __init__(self): super().__ init_() self.initUI() def initUI(self): self.setGeometry(300, 300, 250, 150) self.setWindowTitle ('Event Processing') 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 example, we reimplement the keyPressEvent() event handler.
def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.close()
If we press Esc on the keyboard, the application terminates.
Event sender Event sender
To facilitate distinguishing between multiple event sources connected to the same event target, the sender() method can be used in PyQt5.
#!/ usr/bin/python3# -*- coding: utf-8-*-""PyQt5 tutorial In this example, we identify the object to which the event is sent. Author: My World You have been to blog: http://blog.csdn.net/weiaitaowang Last edited: August 1, 2016 ""import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QPushButtonclass Example(QMainWindow): def __init__(self): super().__ init__() self.initUI() def initUI(self): btn1 = QPushButton ('Button one', self) btn1.move(30, 50) btn2 = QPushButton ('button two', 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 ('Event Send') self.show () def buttonClicked(self): sender = self.sender() self.statusBar().showMessage(sender.text() + 'pressed') 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)
Both buttons are connected to the same slot.
def buttonClicked(self): sender = self.sender() self.statusBar().showMessage(sender.text() + 'pressed')
We determine the signal source by calling the sender() method. In the status bar of the application, the label of the pressed button is displayed.
After the program is executed,
customized emission signal
An object created from a QObject can emit signals. In the following example, we will see how we can customize the signaling.
#!/ /usr/bin/python3# -*- coding: utf-8-*-""PyQt5 tutorial In this example, we show how to emit signals in order to. Author: My World You have been to blog: http://blog.csdn.net/weiaitaowang Last edited: August 1, 2016 ""import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom PyQt5.QtCore import pyqtSignal, QObjectclass 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 ('Transmit Signal') 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 is the launch of the mouse press event. This signal is connected to the close() slot in QMainWindow.
class Communicate(QObject): closeApp = pyqtSignal()
Create a Communicate class that inherits from QObject and has an attribute 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. Application terminated.
The above is all the content of this article "Example Analysis of Events and Signals in PyQt5", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.