PySide——Python图形化界面
PySide——Python图形化界面
PySide——Python图形化界面入门教程(四)
PySide——Python图形化界面入门教程(四)
——创建自己的信号槽
——Creating Your Own Signals and Slots
原文链接:http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
你不必局限于Qt widget提供的信号,你可以使用Signal类来创建自己的信号。下面是一个定义的简单例子:
1 from PySide.QtCore import Signal
2 tapped = Signal()
然后,当对象需要触发信号的条件满足时,你可以使用信号的emit方法,来发出信号调用关联的槽。
thing.tapped.emit()
这样做有两个优点:第一,允许用户和你定义的对象随意交互;第二,让你的对象使用更加灵活,让自己的代码定义动作行为的影响。
一个简单的PySide信号例子
我们来定义一个简单的PunchingBag类,它只做一件事情,当punch被调用时,发出punched信号:

1 from PySide.QtCore import QObject, Signal, Slot
2
3 class PunchingBag(QObject):
4 ''' Represents a punching bag; when you punch it, it
5 emits a signal that indicates that it was punched. '''
6 punched = Signal()
7
8 def __init__(self):
9 # Initialize the PunchingBag as a QObject
10 QObject.__init__(self)
11
12 def punch(self):
13 ''' Punch the bag '''
14 self.punched.emit()

代码非常的简单:PunchingBag继承自QObject,所以它可以发出信号;它有一个称为punched的信号,不携带任何数据;并且他有一个仅仅发出punched信号的punch方法。
为了让PunchingBag更丰富一些,我们需要将它的punched信号和一个槽连接。槽简单的输出“Bag was punched”。

1 @Slot()
2 def say_punched():
3 ''' Give evidence that a bag was punched. '''
4 print('Bag was punched.')
5
6 bag = PunchingBag()
7 # Connect the bag's punched signal to the say_punched slot
8 bag.punched.connect(say_punched)
9
10 # Punch the bag 10 times
11 for i in range(10):
12 bag.punch()

携带数据的PySide信号
创建信号可以完成一个非常有用的事情——携带数据。例如,你可以创建一个携带整数或者字符串的信号:
updated = Signal(int)
updated = Signal(str)
这个数据类型可以是任何Python的类型名或定义了C++类型的字符串。因为教程不假设有任何C++的知识,故我们只使用Python类型。
例子:一个发送信号的圆
我们用x,y和r定义一个圆,x、y是圆中心的坐标,r是半径。我们想要当圆被改变大小时,发送一个信号resized;当圆被移动时,也发送一个信号moved。虽然我们可以在信号的槽中检测圆的大小和位置,但是使用信号发送这些信息会更加方便。
from PySide.QtCore import QObject, Signal, Slot
class Circle(QObject):
''' Represents a circle defined by the x and y
coordinates of its center and its radius r. '''
# Signal emitted when the circle is resized,
# carrying its integer radius
resized = Signal(int)
# Signal emitted when the circle is moved, carrying
# the x and y coordinates of its center.
moved = Signal(int, int)
def __init__(self, x, y, r):
# Initialize the Circle as a QObject so it can emit signals
QObject.__init__(self)
# "Hide" the values and expose them via properties
self._x = x
self._y = y
self._r = r
@property
def x(self):
return self._x
@x.setter
def x(self, new_x):
self._x = new_x
# After the center is moved, emit the
# moved signal with the new coordinates
self.moved.emit(new_x, self.y)
@property
def y(self):
return self._y
@y.setter
def y(self, new_y):
self._y = new_y
# After the center is moved, emit the moved
# signal with the new coordinates
self.moved.emit(self.x, new_y)
@property
def r(self):
return self._r
@r.setter
def r(self, new_r):
self._r = new_r
# After the radius is changed, emit the
# resized signal with the new radius
self.resized.emit(new_r)
注意以下几点:
- Circle继承自QObject所以可以发送信号
- 同样的信号可以在不同地方发送
现在我们定义一些连接Circle的信号的槽。还记得我们上次提过的@Slot修饰符(decorator)吗?现在我们来看看如何接收携带了数据的信号。为了接收信号,我们简单的将其定义为与信号一样的结构。

1 # A slot for the "moved" signal, accepting the x and y coordinates
2 @Slot(int, int)
3 def on_moved(x, y):
4 print('Circle was moved to (%s, %s).' % (x, y))
5
6 # A slot for the "resized" signal, accepting the radius
7 @Slot(int)
8 def on_resized(r):
9 print('Circle was resized to radius %s.' % r)

非常的简单直观。更多信息可以参考Python decorators,或者来学习这篇文章Python Decorators Overview。最后我们完成这个Circle,连接信号槽,移动并改变它的大小。
c = Circle(5, 5, 4)
# Connect the Circle's signals to our simple slots
c.moved.connect(on_moved)
c.resized.connect(on_resized)
# Move the circle one unit to the right
c.x += 1
# Increase the circle's radius by one unit
c.r += 1
当运行脚本的时候,你的结果应该是:
Circle was moved to (6, 5).
Circle was resized to radius 5.
现在我们对信号和槽有了更深入的了解,可以准备使用一些更高级的widgets了。下一个教程开始讨论QListWidget和QListView,两种创建表框(list box)控件的方法。
By Ascii0x03
转载请注明出处:http://www.cnblogs.com/ascii0x03/p/5500933.html
PySide——Python图形化界面的更多相关文章
- PySide——Python图形化界面入门教程(四)
PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...
- PySide——Python图形化界面入门教程(六)
PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...
- PySide——Python图形化界面入门教程(五)
PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...
- PySide——Python图形化界面入门教程(三)
PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...
- PySide——Python图形化界面入门教程(二)
PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...
- PySide——Python图形化界面入门教程(一)
PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...
- 如何使用python图形化界面wxPython
GUI库主要有三类:tkinter,wxPython和PyQt5,下面主要是针对wxPython的使用说明. 下面的操作均在win10 + pycharm上进行 wxPython的安装: pip in ...
- python+pycharm+PyQt5 图形化界面安装教程
python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...
- Git各大平台(win/Linux/Mac)图形化界面客户端大汇总
摘要: 介绍各平台下的图形化界面git客户端(本人并没有全部使用过),欢迎大家补充新的软件或者使用感受~ 一.TortoiseGit - The coolest Interface to Git V ...
随机推荐
- hdu 2757 Ocean Currents(优先队列+bfs)
小伙伴们真心被这道题惊呆了!刚开始是读题,题目都把小伙伴惊呆了,题目都读不懂! 在前面猴子小伙伴的帮助下,理解了一点点,又偷偷的在纸上写写画画,明白了题意! 后来,你懂的,果断拿下!在拿下的过程也经过 ...
- nginx sendfile tcp_nopush tcp_nodelay参数解释
sendfile 现在流行的web 服务器里面都提供 sendfile 选项用来提高服务器性能,那到底 sendfile是什么,怎么影响性能的呢?sendfile实际上是 Linux2.0+以后的推出 ...
- 富文本编辑器 - wangEditor 表情
效果: 文件夹中的表情: 代码:
- java 发送邮件 email相关操作代码测试,生成复杂格式邮件,发送邮件相关操作
项目源码下载:http://download.csdn.net/detail/liangrui1988/6720047 效果图: 相关代码: test1 package com.mail; impor ...
- SQL Server 基础 02 确保数据完整性
本章总结目的: 为了巩固 约束.事务! 约 束 使用数据库约束就是保证数据库的完整性的方法,SQL Server 涉及的完整性有三个: 1.实体完整性 : (不能为空且重复,即唯一的,例如身份证 ...
- [转]CentOS_yum的详细使用方法
yum 是什么yum = Yellow dog Updater, Modified主要功能是更方便的添加/删除/更新RPM包.它能自动解决包的倚赖性问题.它能便于管理大量系统的更新问题 yum特点可以 ...
- 我的Python成长之路---第一天---Python基础(1)---2015年12月26日(雾霾)
2015年12月26日是个特别的日子,我的Python成之路迈出第一步.见到了心目中的Python大神(Alex),也认识到了新的志向相投的伙伴,非常开心. 尽管之前看过一些Python的视频.书,算 ...
- 基于visual Studio2013解决C语言竞赛题之0610冒泡排序函数
题目
- 欢迎大家关注我的微信公众帐号小q机器人(xiaoqrobot)(转)
一个偶然的机会让我接触到了微信公众平台,赶紧加了几个交流群了解下相关情况,突然间发现好像全部的APP开发人员都在研究微信公众帐号的开发,而我显得有些落舞了.至于为什么热度会这么高,我想一个数字足以说明 ...
- mysql设置root密码
1.停止mysql服务.可以在windows服务管理器设置,运行->services.msc->停止mysql服务 2.跳过权限验证: mysqld -nt --skip-grant-ta ...