PySide -- Python graphical interface getting started (3), pysidepython

Source: Internet
Author: User

PySide -- Python graphical interface getting started (3), pysidepython

PySide -- Python graphical interface getting started tutorial (3)

      -- Create an internal number and a slot

-- Using Built-In Signals and Slots

In the previous tutorial, we learned how to create and establish an interactive widgets and how to layout them in two different ways. Today, we will continue to discuss how the Python/Qt application responds to user-triggered events: signals and slots.

When the user executes an action-click the button, select the value of the combo box, and type in the text box-This widget will send a signal. This signal does nothing on its own. It must be connected to the slot. The slot is an object that receives the signal execution action.

 

Connect the built-in PySide/PyQt Signal

Qt widgets has many built-in signals. For example, when QPushButton is clicked, it sends its clicked signal. The clicked signal can be connected to a function with a slot function (just a summary, and more content is required to run)

 1 @Slot() 2 def clicked_slot(): 3     ''' This is called when the button is clicked. ''' 4     print('Ouch!') 5   6   7 # Create the button 8 btn = QPushButton('Sample') 9  10 # Connect its clicked signal to our slot11 btn.clicked.connect(clicked_slot)

Note:@ Slot () decorator is above the definition of clicked_slot (). Although it is not strictly required, it prompts that the C ++ Qt library clicked_slot should be called. (For more information about decorators, see the http://www.pythoncentral.io/python-decorators-overview/.) We will learn more about the @ Slot macro. Now, as long as you know that the button is clicked, it will send a clicked signal and it will call the function connected to it. This function outputs "Ouch!" vividly !".

Let's take a look at QPushButton's three related Signals: pressed, released, and clicked.

 1 import sys 2 from PySide.QtCore import Slot 3 from PySide.QtGui import * 4   5 # ... insert the rest of the imports here 6 # Imports must precede all others ... 7   8 # Create a Qt app and a window 9 app = QApplication(sys.argv)10  11 win = QWidget()12 win.setWindowTitle('Test Window')13  14 # Create a button in the window15 btn = QPushButton('Test', win)16  17 @Slot()18 def on_click():19     ''' Tell when the button is clicked. '''20     print('clicked')21  22 @Slot()23 def on_press():24     ''' Tell when the button is pressed. '''25     print('pressed')26  27 @Slot()28 def on_release():29     ''' Tell when the button is released. '''30     print('released')31  32 # Connect the signals to the slots33 btn.clicked.connect(on_click)34 btn.pressed.connect(on_press)35 btn.released.connect(on_release)36  37 # Show the window and run the app38 win.show()39 app.exec_()

When you click the application button, it will output

pressedreleasedclicked

The pressed signal is sent when the button is pressed, the released signal is sent when the button is released, and finally, after all the actions are completed, the clicked signal is sent.

 

Complete our example Program

Now, it is easy to complete the example program created in the previous tutorial. We add a method to display greeting information for the LayoutExample class.

@Slot()def show_greeting(self):    self.greeting.setText('%s, %s!' %                          (self.salutations[self.salutation.currentIndex()],                          self.recipient.text()))

We use the text () method of recipient QLineEdit to retrieve the text entered by the user. The currentIndex () method of salutation QComboBox gets the user's choice. The Slot () modifier is also used to indicate that show_greeting will be used as a Slot. Then, we connect the clicked signal of the button to it:

self.build_button.clicked.connect(self.show_greeting)

Finally, the example is as follows:

1 import sys 2 from PySide. qtCore import Slot 3 from PySide. qtGui import * 4 5 # Every Qt application must have one and only one QApplication object; 6 # it reads es the command line arguments passed to the script, as they 7 # can be used to customize the application's appearance and behavior 8 qt_app = QApplication (sys. argv) 9 10 class LayoutExample (QWidget): 11 ''' An example of PySide absolute po Sitioning; the main window12 inherits from QWidget, a convenient widget for an empty window. '''13 14 def _ init _ (self): 15 # Initialize the object as a QWidget and16 # set its title and minimum width7 QWidget. _ init _ (self) 18 self. setWindowTitle ('dynamic Greeter ') 19 self. setMinimumWidth (400) 20 21 # Create the QVBoxLayout that lays out the whole form22 self. layout = QVBoxLayout () 23 24 # Create th E form layout that manages the labeled controls25 self. form_layout = QFormLayout () 26 27 self. salutations = ['ahoy', 28 'good Day', 29 'hello', 30 'hei', 31 'hi', 32 'salutes', 33 'wasup ', 34 'yo'] 35 36 # Create and fill the combo box to choose the salutation37 self. salutation = QComboBox (self) 38 self. salutation. addItems (self. salutations) 39 # Add it to the form layout with a label40 self. form_layout. AddRow ('& Salutation:', self. salutation) 41 42 # Create the entry control to specify a43 # recipient and set its placeholder text44 self. recipient = QLineEdit (self) 45 self. recipient. setPlaceholderText ("e.g. 'World' or 'mate' ") 46 47 # Add it to the form layout with a label48 self. form_layout.addRow ('& Recipient:', self. recipient) 49 50 # Create and add the label to show the greeting text51 self. greet Ing = QLabel ('', self) 52 self. form_layout.addRow ('greeting: ', self. greeting) 53 54 # Add the form layout to the main VBox layout55 self. layout. addLayout (self. form_layout) 56 57 # Add stretch to separate the form layout from the button58 self. layout. addStretch (1) 59 60 # Create a horizontal box layout to hold the button61 self. button_box = QHBoxLayout () 62 63 # Add stretch to push the button to the far Right64 self. button_box.addStretch (1) 65 66 # Create the build button with its caption67 self. build_button = QPushButton ('& Build Greeting', self) 68 69 # Connect the button's clicked signal to show_greeting70 self. build_button.clicked.connect (self. show_greeting) 71 72 # Add it to the button box73 self. button_box.addWidget (self. build_button) 74 75 # Add the button box to the bottom of the main VBox la Yout76 self. layout. addLayout (self. button_box) 77 78 # Set the VBox layout as the window's main layout79 self. setLayout (self. layout) 80 81 @ Slot () 82 def show_greeting (self): 83 ''' Show the constructed greeting. ''' 84 self. greeting. setText ('% s, % s! '% 85 (self. salutations [self. salutation. currentIndex ()], 86 self. recipient. text () 87 88 def run (self): 89 # Show the form90 self. show () 91 # Run the qt application92 qt_app.exec _ () 93 94 # Create an instance of the application window and run it95 app = LayoutExample () 96 app. run ()View Code

Run it and you will find that clicking the button will generate greeting information. Now we know how to use the slot we created to connect the built-in signals. In the next tutorial, we will learn how to create and connect our own signals.

 

By Ascii0x03

Reprinted please indicate the source: http://www.cnblogs.com/ascii0x03/p/5499507.html

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.