PySideSimplicissimus Module 4 ShowLicence: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(Change category "LanguageBindings" -> "PySide")
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:PySide]]
 
[[Category:PySide]]
[[Category:LanguageBindings]]
[[Category:LanguageBindings::PySide::Newbie Tutorials]]


* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials]]
* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials]]
Line 13: Line 11:


All current source code for "Close", "About", "Show Licence", all versions of "Combined", "Engineering Application" aka "truss" are stored in one repository '''tuts4pyside'''. For your convenience, please install a copy of the repository with the following command:
All current source code for "Close", "About", "Show Licence", all versions of "Combined", "Engineering Application" aka "truss" are stored in one repository '''tuts4pyside'''. For your convenience, please install a copy of the repository with the following command:
<code> git clone https://github.com/OldAl/tuts4pyside<code>
 
<code> git clone https://github.com/OldAl/tuts4pyside</code>
 
If you do not have git, pllease install it first.
If you do not have git, pllease install it first.
After downloading, place the '''CCPL.txt''' in the same directory as the program. The file has the '''Creative Commons Attribution-Share alike Licence''' text. '''licence.ui''' needs to be converted to Python file as follows:


After downloading, place the '''CCPL.txt''' in the same directory as the program. The file has the '''Creative Commons Attribution-Share alike Licence''' text. '''licence.ui''' needs to be converted to Python file as follows:
<code> pyside-uic licence.ui > ui_licence.py</code>
</code>
 
pyside-uic licence.ui > ui_licence.py
'''Program Listing:''' follows:
<code>
<code>
'''Program Listing:''' follows:
</code>
#!/usr/bin/env python
#!/usr/bin/env python
# licence.py - display GPL licence
# licence.py - display GPL licence
Line 32: Line 31:


class MainWindow(QMainWindow, Ui_MainWindow):
class MainWindow(QMainWindow, Ui_MainWindow):
  def ''init''(self, parent=None):
  def __init__(self, parent=None):
  '''Mandatory initialisation of a class.'''
  '''Mandatory initialisation of a class.'''
  super(MainWindow, self).''init''(parent)
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)
  self.setupUi(self)
  self.showButton.clicked.connect(self.fileRead)
  self.showButton.clicked.connect(self.fileRead)
Line 42: Line 41:
  self.textEdit.setText(open('COPYING.txt').read())
  self.textEdit.setText(open('COPYING.txt').read())


if ''name'' == '''main''':
if __name__ == '__main__':
  app = QApplication(sys.argv)
  app = QApplication(sys.argv)
  frame = MainWindow()
  frame = MainWindow()
  frame.show()
  frame.show()
  app.exec_()
  app.exec_()
</code>
If you run the program and click the button, it will show in the TextEdit window the listing of the licence. All of the code mimics closely that of '''About''' and '''Close''' scripts. Two statements are of some interest:
<code>
<code>
If you run the program and click the button, it will show in the TextEdit window the listing of the licence. All of the code mimics closely that of '''About''' and '''Close''' scripts. Two statements are of some interest:
self.showButton.clicked.connect(self.fileRead)
</code>
</code>
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:
<code>
<code>
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())
</code>
</code>
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:
<code>
<code>
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:
</code>
#open file
#open file
fl = open('COPYING.txt')
fl = open('COPYING.txt')
tmp = fl.read()
tmp = fl.read()
self.textEdit.setText(tmp)
self.textEdit.setText(tmp)
<code>
</code>
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.
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.

Latest revision as of 05:16, 5 June 2016


English [French] 日本語

Show Licence

Please note that this example shows the CCPL license, whereas PySide is released under LGPL. So make sure that you are displaying the right license.

All current source code for "Close", "About", "Show Licence", all versions of "Combined", "Engineering Application" aka "truss" are stored in one repository tuts4pyside. For your convenience, please install a copy of the repository with the following command:

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

If you do not have git, pllease install it first. After downloading, place the CCPL.txt in the same directory as the program. The file has the Creative Commons Attribution-Share alike Licence 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 PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton

from ui_licence import Ui_MainWindow

class MainWindow(QMainWindow, 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__':
 app = QApplication(sys.argv)
 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. 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.