PySide——Python图形化界面

# PySide——Python图形化界面入门教程(四)

PySide——Python图形化界面入门教程(四)

——创建自己的信号槽

——Creating Your Own Signals and Slots

```1 from PySide.QtCore import Signal
2 tapped = Signal()```

`thing.tapped.emit()`

``` 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()```

``` 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()```

`updated = Signal(int)updated = Signal(str)`

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,
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所以可以发送信号
• 同样的信号可以在不同地方发送

```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)```

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.

By Ascii0x03

## PySide——Python图形化界面的更多相关文章

1. PySide——Python图形化界面入门教程(四)

PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...

2. PySide——Python图形化界面入门教程(六)

PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...

3. PySide——Python图形化界面入门教程(五)

PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...

4. PySide——Python图形化界面入门教程(三)

PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...

5. PySide——Python图形化界面入门教程(二)

PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...

6. PySide——Python图形化界面入门教程（一）

PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...

7. 如何使用python图形化界面wxPython

GUI库主要有三类:tkinter,wxPython和PyQt5,下面主要是针对wxPython的使用说明. 下面的操作均在win10 + pycharm上进行 wxPython的安装: pip in ...

8. python+pycharm+PyQt5 图形化界面安装教程

python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...

9. Git各大平台（win／Linux／Mac）图形化界面客户端大汇总

摘要: 介绍各平台下的图形化界面git客户端(本人并没有全部使用过),欢迎大家补充新的软件或者使用感受~  一.TortoiseGit - The coolest Interface to Git V ...

## 随机推荐

做过接口测试的朋友,肯定都知道一个工具--SoapUI,它强大的功能与集成用例的特性,让不管是开发还是测试,都喜欢用它.在经历了众多版本后,SmartBear公司将SoapUI 从进行了大改版,这也是 ...

2. 织梦DEDECMS网站后台安全检测提示 加一个开关

1.进入后台后,点击 系统->系统基本参数->添加变量: 变量名称:cfg_safecheck_switch 变量值:N 变量类型:布尔(Y/N) 参数说明:启用安全监测系统: 2.找到系 ...

3. 转:TinyXM--优秀的C++ XML解析器

读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...

4. <input>标签

一. 上传图片: type="file":上传文件​ accept:选择上传的种类,图片或者是视频(/png只能上传png格式的图片) ​multiple="multip ...

5. MongoDB【第三篇】RockMongo 的安装

第一步:准备 1. 安装 Nginx 参照 Nginx[第一篇]安装 2. 安装 php 参照 PHP[第一篇]安装 3. RockMongo 安装包 rockmongo-v1.0.5.r53.zip ...

6. java 垃圾回收总结（1）

java 垃圾回收总结(1)   以前看过很多次关于垃圾回收相关的文章,都只是看过就忘记了,没有好好的整理一下,发现写文章可以强化自己的记忆. java与C,c++有很大的不同就是java语言开发者不 ...

7. Git 教程（二）：提交和回退