"Simple" Stage 1

We discuss some more the code that we made in Stage 0. Most code was generated by the Qt Designer. Our own coding is very small in scope, but it is also significant.

The Qt Designer produces a file with ".ui" extension, which should be saved in the project directory with other python program files. However, it is not immediately usable, as it needs to be translated into a python file. There are several tools that can do that conveniently. If you are lucky enough to have a good book on Py Qt programming (such as Mark Summerfield's "Rapid GUI Programming with Python and Qt", Prentice Hall, you can read a detailed explanation of different alternatives. I do not know very much about PyQt programming, but all that I have learned about PyQt is in that book!). You can download a tar ball of the examples of that book, which is available on the net. It includes a handy program "makepyqt.pyw". It should be placed just above your project directory and made executable. Then from the project directory use the command:

../makepyqt.pyw

This command will open a gui of makepyqt.pyw. Click on "Build" button and from the file "simple.ui" it will create a Python file "ui_simple.py", which will be directly incorporated into the project. In Stage 0 we discussed another method of generating ui_simple.py, using pyuic4 script.

We are now in a position to start hacking the "simple.py", the main GUI file of your project "simple" - Simple Text Editor. From the previous stage in the wiki pages, the first block of code is

   1 #!/usr/bin/env python
   2 # .../simple_0/simple.py
   3 #  A really simple editor program in PyQt4 - simple.py
   4 
   5 import sys
   6 
   7 from PyQt4.QtGui import (QMainWindow, QApplication)
   8 #from PyQt4.QtCore import *
   9 
  10 from ui_simple import Ui_MainWindow

This is an "import" section: first the sys module and any other system modules. Then we usually have either

   1 from PyQt4.QtGui  import *
   2 from PyQt4.QtCore import *

or preferably the importation statements that specifically list all the modules that are actually used. It is more informative to use this more specific form. You will note that at this stage the QtCore is not in use at all!

You should have a look at the "ui_simple.py" file, but should not change it in any way. You will see that most, if not all, code is in a class Ui_MainWindow. We will use that code that has been kindly generated for us by the "Qt Designer" and the "makepyqt.pyw" program, so we import it with the statement:

   1 from ui_simple import Ui_MainWindow

Next block of code is:

   1 class MainWindow(QMainWindow, Ui_MainWindow):
   2     def __init__(self, parent=None):
   3         super(MainWindow, self).__init__(parent)                                         
   4         self.setupUi(self)

The class declaration is somewhat unusual: the class MainWindow has a double inheritance. That is allowable in Python, but does not happen often. In the current situation the dual inheritance is rather convenient. As a consequence of this dual inheritance, all methods of both MainWindow and Ui_MainWindow are available directly without qualification, so we can readily use self.setupUi. The details of Ui_MainWindow are in the ui_simple.py file and I would urge you to have a good look at it. The init special method is as always the first method to implement. I do not know in detail why the parameter "parent=None" is there and only understand the principle of it. Just do it! The "super" refers to the immediate ancestor of MainWindow. Again, just do it and do not deviate from the pattern, unless you really have a good reason for it and do understand the consequences.

Next statement is rather interesting. Have a look at setupUi() method in the ui_simple.py file. The effect of it is to set up the MainWindow, as the name of the method suggests. Try to run simple.py with the setupUi() method commented out. Not a very satisfactory graphics, are they? So better do not comment out setupUi()!

Finally, we come to the last block of program:

   1 if __name__ == '__main__':
   2     app = QApplication(sys.argv)
   3     frame = MainWindow()
   4     frame.show()
   5     app.exec_()

As in other Python programs, the first line of the above block tells us that the following lines of code will only be executed if this module is run as the mainline. The variable "app" is our choice - we could call it Charlie_Brown equally well, as far as the functionality of our program is concerned. The variable "frame" is again a chosen name. The line "frame.show()" tells us that the frame will be shown. Finally, app.exec_() starts and runs the program loop, which waits for the user events, viz clicks on the (X) to exit or clicks on menu items. An alternative form of the statement that starts the program loop is sys.exit(app.exec_()). This looks a little more complicated, but does the same job - starts the program loop. There are other events that are not programmed. For instance, we can resize the form, we can write in the textEdit window, copy the text to the clipboard, paste text from the clipboard, move the window. All these and many others are events that the program takes care of, without our direct programming.

So our Simple Editor is functional, as far as it goes. The menu items, generally, will not be active. If you were astute enough to use Qt Designer to connect the signal of the Quit action to the slot of Main Window's close() function, then that menu item will in fact close the application.

One should admire the functionality of our application with so little code. Nice as it is, the "editor" is not really useful. At a minimum, we should implement all the menu items - and that is the subject of our following tutorial session!

Return Home

PyQt/simple1 (last edited 2014-06-08 13:19:05 by DavidBoddie)

Unable to edit the page? See the FrontPage for instructions.