English 日本語

Combine All In One

This is the last version of different layout of combine.py program. In this version we will eliminate altogether the combine.ui file and its derivative ui_combine.py file. There is a clear advantage in that – there are fewer files. The disadvantage is that if later we decide to change the appearnce we no longer can have the benefits of using Qt Designer without modifying back the setup. When the program is simply a GUI for an underlying program, it is sometimes worth while to use this modified format. In any case, my favourite GUI design book uses this form, suggesting that it is just as easy to design the forms without visual aids.

As in earlier version, all the required bits of code and other resources are available for a download from the following “repository”:

  1.   git  clone  https://github.com/OldAl/tuts4pyside

The modification of program is implemented by first of all copying the procedure setupUi() from ui_combine_allin1.py into the body of the program itself, combine_allin1.py. Then all the statements are transferred into the procedure init of the main program. That requires several modifications. Fortunately these modifications are very good opportunity to check once understanding of the process of program. We also use this opportunity to determine which widgets we need to import, as we no longer use explicitly the names of QtCore and QtGui in the code. The code looks cleaner then!

Here is the program listing:

  1. #!/usr/bin/env python
  2. # combine_allin1.py - combination of ShowGPL, About, Close scripts
  3. # The purpose of this version of program is to show implementation
  4. # of most code in one file - all_in_1!.
  5.  
  6. import sys
  7. import platform
  8.  
  9. import PySide
  10.  
  11. from PySide.QtCore import QRect
  12.                          QMessageBox,  QIcon, QAction, QWidget, QGridLayout,\
  13.                          QTextEdit, QMenuBar, QMenu, QStatusBar
  14.  
  15. __version__ = '0.0.0'
  16. import qrc_combine
  17.  
  18. class MainWindow(QMainWindow):
  19.     def __init__(self, parent=None):
  20.         super(MainWindow, self).__init__(parent)
  21.         self.resize(731, 475)
  22.         centralwidget = QWidget(self)
  23.         gridLayout = QGridLayout(centralwidget)
  24.         # textEdit needs to be a class variable.
  25.         self.textEdit = QTextEdit(centralwidget)
  26.         gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
  27.         self.setCentralWidget(centralwidget)
  28.         menubar = QMenuBar(self)
  29.         menubar.setGeometry(QRect(0, 0, 731, 29))
  30.         menu_File = QMenu(menubar)
  31.         self.setMenuBar(menubar)
  32.         statusbar = QStatusBar(self)
  33.         self.setStatusBar(statusbar)
  34.         actionShow_GPL = QAction(self)
  35.         actionShow_GPL.triggered.connect(self.showGPL)
  36.         action_About = QAction(self)
  37.         action_About.triggered.connect(self.about)        
  38.         iconToolBar = self.addToolBar("iconBar.png")
  39. #------------------------------------------------------
  40. # Add icons to appear in tool bar - step 1
  41.         actionShow_GPL.setIcon(QIcon(":/showgpl.png"))
  42.         action_About.setIcon(QIcon(":/about.png"))
  43.         action_Close = QAction(self)
  44.         action_Close.setCheckable(False)
  45.         action_Close.setObjectName("action_Close")        
  46.         action_Close.setIcon(QIcon(":/quit.png"))
  47. #------------------------------------------------------
  48. # Show a tip on the Status Bar - step 2
  49.         actionShow_GPL.setStatusTip("Show GPL Licence")
  50.         action_About.setStatusTip("Pop up the About dialog.")
  51.         action_Close.setStatusTip("Close the program.")
  52. #------------------------------------------------------
  53.         menu_File.addAction(actionShow_GPL)
  54.         menu_File.addAction(action_About)
  55.         menu_File.addAction(action_Close)
  56.         menubar.addAction(menu_File.menuAction())
  57.  
  58.         iconToolBar.addAction(actionShow_GPL)
  59.         iconToolBar.addAction(action_About)
  60.         iconToolBar.addAction(action_Close)
  61.         action_Close.triggered.connect(self.close)
  62.              
  63.     def showGPL(self):
  64.         '''Read and display GPL licence.'''
  65.         self.textEdit.setText(open('COPYING.txt').read())
  66.        
  67.     def about(self):
  68.         '''Popup a box with about message.'''
  69.         QMessageBox.about(self, "About PyQt, Platform and the like",
  70.                 """<b> About this program </b> v %s
  71.                <p>Copyright 2011 Your Name.
  72.                All rights reserved in accordance with
  73.                GPL v2 or later - NO WARRANTIES!
  74.                <p>This application can be used for
  75.                displaying OS and platform details.
  76.                <p>Python %s - PySide version %s - Qt version %s on %s""" % \
  77.                 (__version__, platform.python_version(), PySide.__version__,\
  78.                  PySide.QtCore.__version__, platform.system()))      
  79.        
  80. if __name__ == '__main__':
  81.     app = QApplication(sys.argv)
  82.     frame = MainWindow()
  83.     frame.show()
  84.     sys.exit(app.exec_())

No doubt you have noticed that one type of statement is left intact but commented out – these are the statements that include the command setObjetName(‘name_string’). The reason for commenting it out is that these commands are not used in this example at all – the program executes as well without them. They are, however, not just deleted – since they are the direct result of working with the aid of Qt Designer and conversion with pyside-uic script, perhaps they may be useful for certain types of debugging situations. So it does not cost much (actually it costs nothing!) to just comment them out. It is a kind of compromise.

This is the last variation of combine.py that we explore. I think the time is ripe for us to explore a simple engineering problem and use PySide for the GUI design for the underlying program. The problem is very simple – determination of forces in a truss. The method of doing it and data handling is realistic, but not too extensive, so that it is useful for newbie to newbie tutorials. ¡Hasta la vista!
Return to PySideSimplicissimus

Categories: