gpt4 book ai didi

PyQt5每天必学之事件与信号

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章PyQt5每天必学之事件与信号由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

这一部分我们将探索 PyQt5 的事件和信号是如何在应用程序中实现的.

Events事件 。

所有的GUI应用程序都是事件驱动的。应用程序事件主要产生自用户,但它们也可通过其他方法来产生,例如一个互联网连接,一个窗口管理器,或计时器。当我们调用应用程序的exec_()方法,应用程序进入主循环。主循环检测各种事件,并把它们发送到事件对象.

在事件模型中,有三个参与者:

  • event source(事件源)
  • event object(事件对象)
  • event target(事件目标)

事件源是对象的状态改变而产生事件。事件对象(事件)是封装在事件源中状态变化的对象。事件目标是希望被通知的对象。事件源对象代表处理一个事件到事件目标的任务.

PyQt5使用独特的信号和槽机制来处理事件。信号和槽用于对象之间的通信,当一个特定的事件发生时,信号被发射。槽可以是任意的Python调用。信号发射时连接到槽被调用.

Signals & slots信号和槽 。

这是一个简单的例子演示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
#!/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_())

在我们的例子中,将用到QtGui.QLCDNumber和QtGui.QSlider。我们通过拖动滑块改变LCD数字.

?
1
sld.valueChanged.connect(lcd.display)

在这里,滑块的 valueChanged 信号连接到 lcd 的显示(display)槽.

发送器是对象发送信号。接收器是接收信号的对象。槽的是反馈给信号的方法.

程序执行后 。

PyQt5每天必学之事件与信号

覆写系统事件处理程序 。

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

在我们的例子中,我们重新实现 keyPressEvent() 事件处理程序.

?
1
2
3
def keyPressEvent( self , e):
  if e.key() = = Qt.Key_Escape:
  self .close()

如果我们按下键盘上的 Esc 键,应用程序终止.

Event sender事件发送 。

为了方便区分多个连接到同一事件目标的事件源,在PyQt5中可以使用sender()方法.

?
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_())

在我们的例子中有两个按钮。两个按钮都连接 buttonClicked() 方法,我们通过调用 sender() 方法响应单击的按钮.

?
1
2
btn1.clicked.connect( self .buttonClicked)
btn2.clicked.connect( self .buttonClicked)

两个按钮连接到同一个槽.

?
1
2
3
4
def buttonClicked( self ):
 
  sender = self .sender()
  self .statusBar().showMessage(sender.text() + ' 被按下' )

我们通过调用 sender() 方法确定信号源。在应用程序的状态栏,显示被按下按钮的标签.

程序执行后 。

PyQt5每天必学之事件与信号

定制发射信号 。

从一个QObject 创建的对象可以发出信号。在下面的例子中,我们将看看我们如何能够定制发出信号.

?
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_())

我们创建一个名为closeApp新的信号。这个信号是发射按下鼠标事件。该信号被连接到QMainWindow中的close()槽.

?
1
2
class Communicate(QObject):
  closeApp = pyqtSignal()

创建继承自 QObject 的 Communicate 类,该类中有一个 pyqtSignal() 类的属性.

?
1
2
self .c = Communicate()
self .c.closeApp.connect( self .close)

将我们自定义的 closeApp 信号连接到QMainWindow中的close()槽.

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

当我们鼠标在程序窗口出现点击动作时,closeApp信号被发射(emit)。应用程序终止.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/weiaitaowang/article/details/52087770 。

最后此篇关于PyQt5每天必学之事件与信号的文章就讲到这里了,如果你想了解更多关于PyQt5每天必学之事件与信号的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com