- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章PyQt5每天必学之事件与信号由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
这一部分我们将探索 PyQt5 的事件和信号是如何在应用程序中实现的.
Events事件 。
所有的GUI应用程序都是事件驱动的。应用程序事件主要产生自用户,但它们也可通过其他方法来产生,例如一个互联网连接,一个窗口管理器,或计时器。当我们调用应用程序的exec_()方法,应用程序进入主循环。主循环检测各种事件,并把它们发送到事件对象.
在事件模型中,有三个参与者:
事件源是对象的状态改变而产生事件。事件对象(事件)是封装在事件源中状态变化的对象。事件目标是希望被通知的对象。事件源对象代表处理一个事件到事件目标的任务.
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中的处理往往通过重写事件来处理程序.
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() 方法确定信号源。在应用程序的状态栏,显示被按下按钮的标签.
程序执行后 。
定制发射信号 。
从一个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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试将 WPF CodeBehid 事件(如 Event、Handler、EventSetter)转换为 MVVM 模式。我不允许使用 System.Windows.Controls,因为我使用
我可能误解了 Backbone 中的事件系统,但是当我尝试以下代码时什么也没有发生。当我向 Backbone.Events 扩展对象添加新属性时,它不应该触发某种更改、更新或重置事件吗?就像模型一样吗
我遇到了一个简单的问题,就是无法弄清楚为什么它不起作用。我有一个子组件“app-buttons”,其中我有一个输入字段,我想听,所以我可以根据输入值过滤列表。 如果我将输入放在我有列表的根组件中,一切
System.Timers.Timer 的 Elapsed 事件实际上与 System.Windows.Forms.Timer 的 Tick 事件相同吗? 在特定情况下使用其中一种比使用另一种有优势吗
嗨,这个 javascript 代码段是什么意思。(evt) 部分是如此令人困惑.. evt 不是 bool 值。这个怎么运作? function checkIt(evt) { evt
我正在使用jquery full calendar我试图在事件被删除时保存它。 $('calendar').fullCalendar ({
我有两个链接的鼠标事件: $('body > form').on("mousedown", function(e){ //Do stuff }).on("mouseup", function(
这是我的代码: $( '#Example' ).on( "keypress", function( keyEvent ) { if ( keyEvent.which != 44 ) {
我尝试了 dragOver 事件处理程序,但它没有正常工作。 我正在研究钢琴,我希望能够弹奏音符,即使那个键上没有发生鼠标按下。 是否有事件处理程序? 下面是我正在制作的钢琴的图片。 最佳答案 您应该
当悬停在相邻文本上时,我需要使隐藏按钮可见。这是通过 onMouseEnter 和 onMouseLeave 事件完成的。但是当点击另外的文本时,我需要使按钮完全可见并停止 onMouseLeave
我有ul标签内 div标签。我申请了mouseup事件 div标记和 click事件 ul标签。 问题 每当我点击 ul标签,然后都是 mouseup和 click事件被触发。 我想要的是当我点击 u
我是 Javascript 和 jQuery 的新手,所以我有一个非常愚蠢的疑问,请耐心等待 $(document).click(function () { alert("!"); v
我有一个邮政编码解析器,我正在使用 keyup 事件处理程序来跟踪输入长度何时达到 5,然后查询服务器以解析邮政编码。但是我想防止脚本被不必要地调用,所以我想知道是否有一种方法可以跟踪 keydown
使用事件 API,我有以下代码来发布带有事件照片的事件 $facebook = new Facebook(array( "appId" => "XXX", "se
首次加载 Microsoft Word 时,既不会触发 NewDocument 事件也不会触发 DocumentOpen 事件。当 Word 实例已打开并打开新文档或现有文档时,这些事件会正常触发。
我发现了很多相关问题(这里和其他地方),但还没有具体找到这个问题。 我正在尝试监听箭头键 (37-40) 的按键事件,但是当以特定顺序使用箭头键时,后续箭头不会生成“按键”事件。 例子: http:/
给定的 HTML: 和 JavaScript 的: var $test = $('#test'); $test.on('keydown', function(event) { if (eve
我是 Node.js 的新手,希望使用流运行程序。对于其他程序,我必须同时启动一个服务器(mongodb、redis 等),但我不知道我是否应该用这个运行一个服务器。请让我知道我哪里出了问题以及如何纠
我正在尝试使用 Swift 和 Cocoa 创建一个适用于 OS X 的应用程序。我希望应用程序能够响应关键事件,而不将焦点放在文本字段上/文本字段中。我在 Xcode 中创建了一个带有 Storyb
我有以下代码: (function(w,d,s,l,i){ w[l]=w[l]||[];w[l].push({
我是一名优秀的程序员,十分优秀!