- Note: this article is a member of the multipart PySide_Newbie_Tutorials
English French [qt-devnet.developpez.com] 日本語
Alternative Combine
We call this program combine_alter.py. Actually it does no more and no less than the combine.py program, though it does it somewhat differently – it circumvents the multiple inheritance. Though multiple inheritance is a nice Python feature, many programmers, particularly those migrating from other language may well prefer to avoid it, because Python is almost unique in its support of multiple inheritance (note that C++ does support it, but not Java, C# or other usual object-oriented languages). Though we started of with multiple inheritance, some may well prefer this or some other alternative way. By using the name of combine_alter.py we show that its difference from combine.py by appending “_alter” to the name. The combine.ui and qrc_combine.py programs remain unaltered, so we simply use them without bothering to make it available from an upload. The program itself is available from the usual place
- http://akabaila.pcug.org.au/pyside-data/combine_alter.py and http://akabaila.pcug.org.au/pyside-data/combine/
The combine_alter.py is not much different from combine.py. We change the import statement for ui_combine, though the change is really aesthetic only. A local variable ui is introduced early in the initialisation statement. As it turns out it used also outside the init function, as an afterthought we store it also as class variable. This should be very familiar to Python programmers, as more often then not the arguments to the init function are received as local variables and promptly stored as class variables.
- #!/usr/bin/env python
- # combine_alter.py - combination of ShowGPL, About, Close scripts
- import sys
- import platform
- import PySide
- __version__ = '0.0.0'
- from ui_combine import Ui_MainWindow as Ui
- import qrc_combine
- def __init__(self, parent=None):
- super(MainWindow, self).__init__(parent)
- ui = Ui()
- # Store ui as class variable self.ui
- self.ui = ui
- ui.setupUi(self)
- ui.actionShow_GPL.triggered.connect(self.showGPL)
- ui.action_About.triggered.connect(self.about)
- iconToolBar = self.addToolBar("iconBar.png")
- #------------------------------------------------------
- # Add icons to appear in tool bar - step 1
- #------------------------------------------------------
- # Show a tip on the Status Bar - step 2
- ui.actionShow_GPL.setStatusTip("Show GPL Licence")
- ui.action_About.setStatusTip("Pop up the About dialog.")
- ui.action_Close.setStatusTip("Close the program.")
- #------------------------------------------------------
- iconToolBar.addAction(ui.actionShow_GPL)
- iconToolBar.addAction(ui.action_About)
- iconToolBar.addAction(ui.action_Close)
- def showGPL(self):
- '''Read and display GPL licence.'''
- self.ui.textEdit.setText(open('COPYING.txt').read())
- def about(self):
- '''Popup a box with about message.'''
- """<b> About this program </b> v %s
- <p>Copyright © 2010 Joe Bloggs.
- All rights reserved in accordance with
- GPL v2 or later - NO WARRANTIES!
- <p>This application can be used for
- displaying OS and platform details.
- <p>Python %s - PySide version %s - Qt version %s on %s""" % \
- (__version__, platform.python_version(), PySide.__version__,\
- if __name__ == '__main__':
- frame = MainWindow()
- frame.show()
- app.exec_()
Basically, it is a matter of taste which method one uses – with multiple inheritance or without. Anyhow, it may well be easier ot adopt one or the other. Even though, it is good to have some familiarity with both methods, as one meets both when reading programs.
Enjoy the slightly different presentation of this very basic program setup! Return to PySideSimplicissimus

