First PySide application
It's time to roll up our sleeves and get our hands dirty with some real coding now. We are going to learn how to create our first and the 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 may look a 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 * from PySide.QtGui import * # 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!!!\n 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()
On interpretation, you will get an output window, as shown in the figure:
Now, let's get into the working of the code. We start with importing the necessary objects into the program.
Lines 1, 2 and 3 imports the necessary modules that are required for the program. Python is supported with a library of standard modules that are built into the interpreter and provide access to operations that are not a 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 the sys
module to pass command-line arguments sys.argv
as a parameter to the QApplication
class. It contains the list of command-line arguments that are passed to a Python script. Any basic GUI application that uses PySide should have two classes imported for 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 to create and modify 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 its internal state, accordingly. There should be only one QApplication
object in the whole application even though the application creates one or many windows at any point in time.
Tip
The QApplication
object must be created before the creation of any other objects as this handles system-wide and application-wide settings for your application. It is also advised to create it before any modification of command-line arguments is 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 next 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.
As 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 an object-oriented paradigm that supports these features. QLabel
is a base class that is inherited from the QFrame
super class 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. At this point only, the label becomes visible to the user and they are able to view it 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().