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

``` 1 from PySide.QtCore import QObject, Signal, Slot
2
3 class Circle(QObject):
4     ''' Represents a circle defined by the x and y
5         coordinates of its center and its radius r. '''
6     # Signal emitted when the circle is resized,
7     # carrying its integer radius
8     resized = Signal(int)
9     # Signal emitted when the circle is moved, carrying
10     # the x and y coordinates of its center.
11     moved = Signal(int, int)
12
13     def __init__(self, x, y, r):
14         # Initialize the Circle as a QObject so it can emit signals
15         QObject.__init__(self)
16
17         # "Hide" the values and expose them via properties
18         self._x = x
19         self._y = y
20         self._r = r
21
22     @property
23     def x(self):
24         return self._x
25
26     @x.setter
27     def x(self, new_x):
28         self._x = new_x
29         # After the center is moved, emit the
30         # moved signal with the new coordinates
31         self.moved.emit(new_x, self.y)
32
33     @property
34     def y(self):
35         return self._y
36     @y.setter
37     def y(self, new_y):
38         self._y = new_y
39         # After the center is moved, emit the moved
40         # signal with the new coordinates
41         self.moved.emit(self.x, new_y)
42
43     @property
44     def r(self):
45         return self._r
46
47     @r.setter
48     def r(self, new_r):
49         self._r = new_r
50         # After the radius is changed, emit the
51         # resized signal with the new radius
52         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)```

``` 1 c = Circle(5, 5, 4)
2
3 # Connect the Circle's signals to our simple slots
4 c.moved.connect(on_moved)
5 c.resized.connect(on_resized)
6
7 # Move the circle one unit to the right
8 c.x += 1
9
10 # Increase the circle's radius by one unit
11 c.r += 1```

```Circle was moved to (6, 5).
Circle was resized to radius 5.```

37 篇文章26 人订阅

0 条评论

## 相关文章

### 一致性（连续性）hash算法（Consistent hashing）一致性（连续性）hash算法（Consistent hashing）

Consistent hashing is a scheme that provides hash table functionality in a way t...

14920

38250

1K00

10940

50040

59870

89670

21150

30760

20980