- Note: this article is a member of the multipart PySide Newbie Tutorials
English French [qt-devnet.developpez.com] 日本語
Show Licence
Please note that this example shows the GPL license, whereas PySide is released under LGPL. So make sure that you are displaying the right license.
This time we have to store in an external repository for your convenience several files: COPYING..txt, licence.ui and licence.py. All those files are in the following location: COPYING.txt [akabaila.pcug.org.au], licence.ui [akabaila.pcug.org.au] and licence.py [akabaila.pcug.org.au].
After downloading, place the COPYING.txt in the same directory as the program. The file has the GPL v 2 text. licence.ui needs to be converted to Python file as follows:
- pyside-uic licence.ui > ui_licence.py
Program Listing: follows:
- #!/usr/bin/env python
- # licence.py - display GPL licence
- import sys
- from ui_licence import Ui_MainWindow
- def __init__(self, parent=None):
- '''Mandatory initialisation of a class.'''
- super(MainWindow, self).__init__(parent)
- self.setupUi(self)
- self.showButton.clicked.connect(self.fileRead)
- def fileRead(self):
- '''Read and display GPL licence.'''
- self.textEdit.setText(open('COPYING.txt').read())
- if __name__ == '__main__':
- frame = MainWindow()
- frame.show()
- app.exec_()
If you run the program and click the button, it will show in the TextEdit window the listing of the licence. Clcik following link to l show you an image, stored externally:
- http://akabaila.pcug.org.au/pyside-images/licence.png
All of the code mimics closely that of About and Close scripts. Two statements are of some interest:
- self.showButton.clicked.connect(self.fileRead)
The purpose of the pushButton is to show the licence, so it has been named showButton. The above statement connects the showButton.clicked event with a class method fileRead. Thus when showButton is clicked, the Python class method fileRead is executed. That method has a single “Pythonised” statement:
- self.textEdit.setText(open('COPYING.txt').read())
Not the simplest way to show what happens, but Python programmers like to do such things to us, readers. The code is equivalent to a series of statements as follows:
- #open file
- fl = open('COPYING.txt')
- tmp = fl.read()
- self.textEdit.setText(tmp)
It may be clearer, but a lot more typing and somewhat slower as Python is an interpreter. It is a subjective judgement which of the two styles is better.
Return to PySideSimplicissimus

