Sunday, November 30, 2008

My PyQt Scribbles (Python and Qt) #2: a plain window in PyQt

Today we will create a main window, set a window title, an icon and a tooltip related to the window.
First of all let's check that our PyQt is well installed and working. To do this just type: import PyQt4 from IDLE and press enter. If nothing happens we're fine. If it shows an error message like "ImportError: No module named PyQt4", try to check your installation (see My PyQt scribbles #1).
If everything is fine we can start.
I will first present the code as a whole and then proceed through it to analyse the various parts.
import sys
from PyQt4 import QtGui

class HelloWorldWindow(QtGui.QMainWindow):
        def __init__(self, parent = None):
                QtGui.QMainWindow.__init__(self, parent)
                self.setWindowTitle("My First Qt Window")
                self.setGeometry(300,300,250,150)
                self.setWindowIcon(QtGui.QIcon("C:/Python26/PyQt/Icon/gadu.png"))
                self.setToolTip("Hello to this Wiz and Chips example!")

app = QtGui.QApplication(sys.argv)
main_window = HelloWorldWindow()
main_window.show()
app.exec_()
We begin importing the needed modules.
import sys
from PyQt4 import QtGui

Sys is needed to initialise the QApplication class. This class manages the application control flow and the main settings. QApplication contains the main event loop (where all events from the window system and other sources are processed and dispatched), the application initialisation and finalization. For any GUI application based on Qt there must be one QApplication object, no matter how many windows are displayed.
QtGui is the module which contains the GUI classes. As a matter of fact extends the QtCore module with GUI functionality
We now create a main class:
class HelloWorldWindow(QtGui.QMainWindow):
The argument of our window class refers to another class, QMainWindow, which supplies a main application window.


The layout has a center widget area where any kind of widget can be placed. You can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. It's not currently possible to create a main window without a central widget. You must have a central widget even if it is just a placeholder.
Now we create a function to initialise the class
def __init__(self, parent = None):
            QtGui.QMainWindow.__init__(self, parent)
this is to initialise the main window. __init__ is a special function in python which is used to set up the object using the arguments to make variables for the object. __init__ is called upon the object creation.
Now it's time to shape a bit our window.
self.setWindowTitle("My First Qt Window")
The setWindowTitle() method sets the window title
self.setGeometry(300,300,250,150)
The setGeometry method sets the relative position (x,y) on the screen and the size (w,h) of the window
self.setWindowIcon(QtGui.QIcon("C:/Python26/PyQt/Icon/gadu.png"))
This method sets the window icon. For my test I have used a crystal clear project icon (http://www.everaldo.com/crystal/). For windows user be sure to use "/" as folder separator instead of "\". The latter is used for escapes in python.
self.setToolTip("Hello to this Wiz and Chips example!")
With this method we specify a tooltip related to our present object: the main window
app = QtGui.QApplication(sys.argv)
QApplication class manages the GUI control flow and main settings
main_window = HelloWorldWindow()
main_window.show()

The show() method displays the widget on the screen
app.exec_()
This is to enter the main loop
Here below you can see the result: a window with all the accessories we have defined both in Windows XP and Linux Kubuntu 8.10 KDE 4.1 fashion.



Pretty neat result isn't it? And without too much effort also! At this very early point PyQt seems quite cool and easy to me. Let's see what we can pull out of it.
As always.. stay tuned folks!
Disclaimer: I'm not a prefessional python programmer nor I pretend to be. I am just an amateur willing to learn and share its by-no-means-guarantee-as-it know-how with the reader. I nevertheless hope you will enjoy my scribbles and maybe find them somewhat stimulating and helpful.

No comments:

Post a Comment