"Simple Finished"
Preamble
If you want just to run the example, you are advised to:
Read at lest the prerequisites section of Stage 0
Download and expand the tar ball "simple0.1.00.tar.gz" from http://akabaila.pcug.org.au/data_sample/ It has the required resource files.
Introduction
It is usual to present tutorials with part of the project and only then the whole. It seems to me that it is not the best way. I feel that it is better to first state the objective and show what the finished project will look like and only then take it in small steps, showing at each step what has been achieved.
The aim of this tutorial is to show how a simple, yet reasonably complete, plain text editor can be created using the PyQt. We shall call that simple editor, rather appropriately, "Simple". As we are first presenting the finished project, some readers will want to see how it was done and try it out themselves. They are urged to look at each Stage 0 first, then Stage 1 and so on. Please try to code each stage yourselves, as you will gain most out of doing, rather than reading.
Other readers my find that the example is too simple for them and they are looking for some specific, advanced technique. By making discovering this in early reading of this tutorial they will be saved unnecessary looking through this presentation that is too simple and pace that is too slow. The motto for this tutorial is "from a newbie to other newbies" and we apologise up front if it does not apply to you.
"Simple" Finished
Finished is a relative term - there are features which could usefully be added to this GUI program, but they may well be contrary to the aim of keeping the example as simple as possible.
The main item in that category is saving the program state at the time of closing it and remembering the state when next opening the program. The technique to do that is well described in chapter 6 of the book "Rapid GUI Programming with Python and Qt" by Mark Summerfield. It is a fine book well worth a place on every PyQt programmers desk. The omission of this feature is partly prompted by similar omissions from some "standard" programs, notably "Dolphin" file manager. Personally when I use "Dolphin", I wish that it did "remember" where we were at in the previous session... But it it is good enough for systems programmers to forget about it, it may be good enough for a tutorial example that aims at simplicity!
One other feature requires qualification: the "help" files are installed in the "qrc" resources. That is convenient, but also doubtful in view of its lack of economy of RAM. It seems that the resource file is automatically loaded in memory at the start of the program. That my be fine today when the memory is measured in Giga Bytes. Fine, provided that the program does not require large memory for, say, equations solving, a feature of programs for Structural Analysis of Engineering Structure, my area of interest. So personally, I would prefer to save resources of RAM and leave the help system in html format, particularly since help system, IMHO, is never too extensive.
Image of "Simple"
Program Listing of "Simple"
1 #!/usr/bin/env python
2 # .../simple.py - developing of a simple text editor.
3 '''A really simple editor program in PyQt4 - simple.py
4 '''
5
6 import sys
7 import os
8 import platform
9
10 from PyQt4.QtGui import (QMainWindow, QApplication, QFileDialog,
11 QKeySequence, QAction, QIcon, QMessageBox)
12 from PyQt4.QtCore import (SIGNAL, PYQT_VERSION_STR, QT_VERSION,
13 QT_VERSION_STR)
14
15 from ui_simple import Ui_MainWindow
16 import qrc_simple
17 import helpform
18
19 __version__ = "0.1.00"
20
21 class MainWindow(QMainWindow, Ui_MainWindow):
22 def __init__(self, parent=None):
23 super(MainWindow, self).__init__(parent)
24 self.setupUi(self)
25 self.action_New = self.editAction(self.action_New, self.fileNew,
26 QKeySequence.New, "filenew",
27 'Clear the textEdit window for a new file.')
28 self.action_Open = self.editAction(self.action_Open, self.fileOpen,
29 QKeySequence.Open, "fileopen", "Open an existing file")
30 self.action_Save = self.editAction(self.action_Save, self.fileSave,
31 QKeySequence.Save, "filesave", "Save file")
32 self.actionSave_As = self.editAction(self.actionSave_As, self.fileSaveAs,
33 "Ctrl+A", "filesaveas", "Save file with a new name")
34 self.action_Quit = self.editAction(self.action_Quit, self.fileQuit,
35 "Ctrl+Q", "filequit", "Close main window and application")
36 self.fileName = None
37 fileToolbar = self.addToolBar("File")
38 self.addActions(fileToolbar, (self.action_New, self.action_Open,\
39 self.action_Save, self.actionSave_As))
40