Pyside--python graphical interface

Source: Internet
Author: User

Pyside--python graphical interface

Pyside--python Graphical Interface Introductory tutorial (iv)

Pyside--python Graphical Interface Introductory tutorial (iv)

--Create your own signal slots

--creating Your Own Signals and Slots

Original link: http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/

You don't have to confine yourself to the signals provided by the QT widget, you can use the signal class to create your own signals. Here is a simple example of a definition:

1 from pyside.qtcore import Signal2 tapped = Signal ()

Then, when the object needs to trigger a signal, you can use the emit method of the signal to emit a signal to call the associated slot.

Thing.tapped.emit ()

There are two advantages to this: First, allow the user to interact freely with the objects you define, and second, make your objects more flexible and let your code define the effects of action behavior.

An example of a simple pyside signal

Let's define a simple punchingbag class that only does one thing, and when Punch is called, emits a punched signal:

1 from Pyside.qtcore import Qobject, Signal, Slot 2   3 class Punchingbag (Qobject): 4     "represents a punching bag; When you punch it, it is 5         emits a signal that indicates it is punched. "' 6     punched = Signal () 7   8     def __init__ (self): 9         # Initialize The punchingbag as a QObject10         qobject . __init__ (self) One-off     def Punch (self):         "Punch the Bag" "         self.punched.emit ()

The code is very simple: Punchingbag inherits from Qobject, so it can send a signal, it has a signal called punched, does not carry any data, and he has a punch method that simply emits a punched signal.

To make the punchingbag richer, we need to connect its punched signal to a slot. Slot Simple Output "Bag was punched".

1 @Slot () 2 def say_punched (): 3     "Give evidence that a bag is punched. ' 4     print (' bag was punched. ') 5   6 bag = Punchingbag () 7 # Connect The Bag ' s punched signal to the say_punched SL OT 8 Bag.punched.connect (say_punched) 9 # Punch The bag of TIMES11 for me in range:     bag.punch ()

Pyside signal for carrying data

Creating a signal can do a very useful thing-carrying data. For example, you can create a signal that carries an integer or a string:

Updated = Signal (int)
Updated=Signal(str)

This data type can be any Python type name or a string that defines a C + + type. Because tutorials do not assume any knowledge of C + +, we only use Python types.

Example: a circle that sends a signal

We use X, Y and R to define a circle, X, y are the coordinates of the Circle center, and R is the radius. We want to send a signal resized when the circle is changed size, and send a signal moved when the circle is moved. Although we can detect the size and position of the circle in the slot of the signal, it is more convenient to use the signal to send the information.

View Code

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)

Note the following points:

    • Circle inherits from Qobject so it can send a signal.
    • The same signal can be sent in different places.

Now we define some slots that connect the signals of circle. Remember the @slot modifier (decorator) We mentioned last time? Now let's take a look at how to receive the signals that carry the data. To receive the signal, we simply define it as a signal-like structure.

1 # A slot for the "moved" signal, accepting the X and y coordinates2 @Slot (int, int) 3 def on_moved (x, y): 4     print (' Cir CLE is moved to (%s,%s). '% (x, y) 5  6 # A slots for the "resized" signal, accepting the RADIUS7 @Slot (int) 8 def on_r Esized (R): 9     print (' Circle is resized to radius%s. '% r)

Very simple and intuitive. For more information, refer to Python decorators, or learn more about this article, Python decorators overview. Finally we complete this circle, connect the signal slots, move and change its size.

View Code

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

When you run the script, your results should be:

View Code
Circle is moved to (6, 5). Circle is resized to radius 5.

Now that we have a deeper understanding of the signals and slots, we are ready to use some of the more advanced widgets. The next tutorial begins with the discussion of Qlistwidget and Qlistview, two ways to create a table box control.

by ascii0x03

Reprint Please specify source: http://www.cnblogs.com/ascii0x03/p/5500933.html

Pyside--python graphical interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.