My first PySide application

Let's get our hands dirty with some real coding now. We are going to learn how to create our first and traditional "Hello World" application. Have a look at the code first and we will dissect the program line-by-line for a complete explanation of what it does. The code might look little strange to you at first but you will gain understanding as we move through.

# Import the necessary modules required
import sys
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QLabel
# Main Function
if __name__ == '__main__':

    # Create the main application
    myApp = QApplication(sys.argv)

    # Create a Label and set its properties
    appLabel = QLabel()
    appLabel.setText("Hello, World!!!
 Look at my first app using PySide")
    appLabel.setAlignment(Qt.AlignCenter)
    appLabel.setWindowTitle("My First Application")
    appLabel.setGeometry(300, 300, 250, 175)

    # Show the Label
    appLabel.show()

    # Execute the Application and Exit
    myApp.exec_()
    sys.exit()

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

On execution, you get an output window as shown in the following screenshot:

My first PySide application

Now, let's get into the working of the code. We start with importing the necessary objects into the program.

The first three in the code snippet import the modules required for the program. Python is supported with a library of standard modules which are built into the interpreter and provide access to operations that are not part of the core language. One such standard module is sys which provides access to some variables and functions that are used closely by the interpreter. In the preceding program, we need sys module to pass command line arguments sys.argv as a parameter to the QApplication class. It contains the list of command line arguments passed to a Python script. In most of the GUI applications that use PySide, we might have two classes imported for a basic functionality. They are QtCore and QtGui. The QtCore module contains functions that handle signals and slots and overall control of the application whereas QtGui contains methods for creating and modifying various GUI window components and widgets.

In the main program, we are creating an instance of the QApplication class. QApplication creates the main event loop, where all events from the window system and other sources are processed and dispatched. This class is responsible for an application's initialization, finalization, and session management. It also handles the events and sets the application's look and feel. It parses the command line arguments (sys.argv) and sets the internal state accordingly. There should only be one QApplication object in the whole application even though it creates one or many windows at any point in time.

Tip

The QApplication object must be created before any other objects as this handles the system-wide and application-wide settings for your application. It is also advised to create it before any modification of command line arguments received.

Once the main application instance is created, we move on by creating a QLabel instance that will display the required message on the screen. This class is used to display a text or an image. The appearance of the text or image can be controlled in many ways by the functions provided by this class. The two lines that follow the instantiation of this class set the text to be displayed and align it in a way that is centered on the application window.

Since Python is an object-oriented programming language, we take the advantage of many object-oriented features such as polymorphism, inheritance, object initialization, and so on. The complete Qt modules are designed in object-oriented paradigm that supports these features. QLabel is a base class inherited from the super class QFrame whose parent class is QWidget (the details will be covered in forthcoming chapters). So, the functions that are available in QWidget and QFrame are inherited to QLabel. The two functions setWindowTitle and setGeometry are functions of QWidget which are inherited by the QLabel class. These are used to set the title of the window and position it on the screen.

Now that all the instantiation and setup is done, we are calling the show function of the QLabel object to present the label on the screen. It is only at this point that the label becomes visible to the user on the screen. Finally, we call the exec_() function of the QApplication object which will enter the Qt main loop and start executing the Qt code. In reality, this is where the label will be shown to the user but the details can be safely ignored as of now. Finally, we exit the program by calling sys.exit().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163