Python GUI PySide

20
Python Desktop GUI - PySide Eueung Mulyana http://eueung.github.io/python/pyside Python CodeLabs | Attribution-ShareAlike CC BY-SA 1 / 20

Transcript of Python GUI PySide

Page 1: Python GUI  PySide

Python Desktop GUI - PySideEueung Mulyana

http://eueung.github.io/python/pysidePython CodeLabs | Attribution-ShareAlike CC BY-SA

1 / 20

Page 2: Python GUI  PySide

Basics

2 / 20

Page 3: Python GUI  PySide

Example #1

from PySide.QtGui import QApplication, QLabel#--------------------------------------app = QApplication([])#--------------------------------------window = QLabel('Window from label')window.show()#--------------------------------------app.exec_()

Example #2

from PySide.QtGui import QApplication, QPushButton#--------------------------------------app = QApplication([])#--------------------------------------button = QPushButton('Exit')button.clicked.connect(app.exit)button.show()#--------------------------------------app.exec_()

3 / 20

Page 4: Python GUI  PySide

Example #3

Example #4

import sys#--------------------------------------from PySide.QtGui import QApplication, QLabel#--------------------------------------app = QApplication(sys.argv)label = QLabel("<font color=red size=40>Hello World</font>")label.show()#--------------------------------------app.exec_()sys.exit()

import sysimport PySidefrom PySide.QtGui import QApplication, QMessageBox#--------------------------------------app = QApplication(sys.argv)#--------------------------------------msgBox = QMessageBox()msgBox.setText("Hello World - using PySide version " + PySide.__version__)msgBox.exec_()

4 / 20

Page 5: Python GUI  PySide

Example #5 

from PySide.QtGui import QApplication, QPushButton, QColorDialog, QMessageBox, QPixmap#--------------------------------------def choose_color(): color = QColorDialog().getColor()

msgbox = QMessageBox() if color.isValid(): pixmap = QPixmap(50, 50) pixmap.fill(color) msgbox.setWindowTitle(u'Selected Color') msgbox.setIconPixmap(pixmap) else: msgbox.setWindowTitle(u'No Color was Selected') msgbox.exec_()#--------------------------------------app = QApplication([])#-------------------------------------- button = QPushButton('Choose Color')button.clicked.connect(choose_color)button.show()#--------------------------------------app.exec_()

5 / 20

Page 6: Python GUI  PySide

Example #6

from PySide.QtGui import QApplication, QPushButton, QColorDialog, QMessageBox#--------------------------------------class ButtonPainter(object): def __init__(self, button): self.button = button

def choose_color(self): color = QColorDialog().getColor()

if color.isValid(): self.button.setStyleSheet(u'background-color:' + color.name()) else: msgbox = QMessageBox() msgbox.setWindowTitle(u'No Color was Selected') msgbox.exec_()#--------------------------------------app = QApplication([])#-------------------------------------- button = QPushButton('Choose Color')button_painter = ButtonPainter(button)button.clicked.connect(button_painter.choose_color)button.show()#--------------------------------------app.exec_()

6 / 20

Page 8: Python GUI  PySide

Example #1pyside-uic quitter.ui -o ui_quitter.py

import sysfrom PySide.QtGui import QMainWindow, QPushButton, QApplicationfrom ui_quitter import Ui_MainWindow#----------------------------------class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self)#----------------------------------if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() app.exec_()

8 / 20

Page 9: Python GUI  PySide

Qt4 Designer - quitter.ui9 / 20

Page 10: Python GUI  PySide

Example #2pyside-uic about.ui > ui_about.py

import sysimport platform#----------------------------------import PySidefrom PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton, QMessageBox#----------------------------------from ui_about import Ui_MainWindow#----------------------------------__version__ = '0.0.1'#----------------------------------class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.aboutButton.clicked.connect(self.about) #---------------------------------- def about(self): QMessageBox.about(self, "About PySide, Platform and the like" """<b>Platform Details</b> v {} <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 platform details. <p>Python {} - PySide version {} - Qt version {} on {}""".format(__version__, platform.python_version(), PySide.__version__, PySide.QtCore.__version__, platform.system()))#----------------------------------if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() app.exec_()

10 / 20

Page 11: Python GUI  PySide

Qt4 Designer - about.ui11 / 20

Page 12: Python GUI  PySide

Example #3pyside-uic licence.ui > ui_licence.py

import sysfrom PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButtonfrom ui_licence import Ui_MainWindow#----------------------------------__version__ = '3.3.0'#----------------------------------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 licence.''' with open('CCPL.txt') as nonamefile: self.textEdit.setText(nonamefile.read())#----------------------------------if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() app.exec_()

12 / 20

Page 13: Python GUI  PySide

Qt4 Designer - licence.ui13 / 20

Page 14: Python GUI  PySide

Example #4pyside-uic combine.ui > ui_combine.py

import sysimport platform#---------------------------------------------import PySidefrom PySide.QtGui import QApplication, QMainWindow, QMessageBox#---------------------------------------------__version__ = '3.0.3'from ui_combine import Ui_MainWindow#---------------------------------------------class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.actionShow_CCPL.triggered.connect(self.showCCPL) self.action_About.triggered.connect(self.about) def showCCPL(self): with open('CCPL.txt') as fi: self.textEdit.setText(fi.read()) def about(self): QMessageBox.about(self, "About PySide, Platform and version." """<b> about.py version %s </b> <p>Copyright © 2013 by Algis Kabaila. This work is made available under the terms of Creative Commons Attribution-ShareAlike 3.0 license, http://creativecommons.org/licenses/by-sa/3.0/. <p>This application is useful for displaying Qt version and other details. <p>Python %s - PySide version %s - Qt version %s on %s""" % (__version__, platform.python_version(), PySide.__version__, PySide.QtCore.__version__, platform.system()))#---------------------------------------------if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() sys.exit(app.exec_())

14 / 20

Page 15: Python GUI  PySide

Qt4 Designer - combine.ui15 / 20

Page 16: Python GUI  PySide

Example #5pyside-rcc combine.qrc -o qrc_combine.py

combine.qrc

<!DOCTYPE RCC><RCC version="1.0"><qresource><file alias="quit.png">select_tango/32x32/actions/system-log-out.png<file alias="about.png">select_tango/32x32/apps/preferences-system-session.png<file alias="showgpl.png">select_tango/32x32/apps/help-browser.png</qresource></RCC>

16 / 20

Page 17: Python GUI  PySide

import sysimport platformimport PySidefrom PySide.QtGui import (QApplication, QMainWindow, QMessageBox, QIcon)#-----------------------------__version__ = '3.1.5'from ui_combine import Ui_MainWindowimport qrc_combine#-----------------------------class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.setWindowTitle('Combine Code Blocks.') self.actionShow_CCPL.triggered.connect(self.showCCPL) self.action_About.triggered.connect(self.about) iconToolBar = self.addToolBar('') #------------------------------------------------------# Add icons to appear in tool bar - step 1 self.actionShow_CCPL.setIcon(QIcon(":/showgpl.png")) self.action_About.setIcon(QIcon(":/about.png")) self.action_Close.setIcon(QIcon(":/quit.png"))#------------------------------------------------------# Show a tip on the Status Bar - step 2 self.actionShow_CCPL.setStatusTip("Show CCP Licence" self.action_About.setStatusTip("Pop up the About dialog." self.action_Close.setStatusTip("Close the program.")#------------------------------------------------------ iconToolBar.addAction(self.actionShow_CCPL) iconToolBar.addAction(self.action_About) iconToolBar.addAction(self.action_Close)

Example #5 def showCCPL(self): # ...

def about(self): # ... #----------------------------- if __name__ == '__main__': # ...

17 / 20

Page 18: Python GUI  PySide

import ...from PySide.QtGui import (QApplication, QMainWindow, QMessageBox, QIcon)#------------------------------------------------------__version__ = '3.1.5'from ui_combine import Ui_MainWindow as Uiimport qrc_combine#------------------------------------------------------ class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # Store Ui() as class variable self.ui self.ui = Ui() self.ui.setupUi(self) self.setWindowTitle('Combine Code Blocks.') self.ui.actionShow_CCPL.triggered.connect(self.showCCPL) self.ui.action_About.triggered.connect(self.about) iconToolBar = self.addToolBar("iconBar.png") #------------------------------------------------------# Add icons to appear in tool bar - step 1 self.ui.actionShow_CCPL.setIcon(QIcon(":/showgpl.png" self.ui.action_About.setIcon(QIcon(":/about.png")) self.ui.action_Close.setIcon(QIcon(":/quit.png"))#------------------------------------------------------# Show a tip on the Status Bar - step 2 self.ui.actionShow_CCPL.setStatusTip("Show CC Licence" self.ui.action_About.setStatusTip("Pop up the About dialog." self.ui.action_Close.setStatusTip("Close the program."#------------------------------------------------------ iconToolBar.addAction(self.ui.actionShow_CCPL) iconToolBar.addAction(self.ui.action_About) iconToolBar.addAction(self.ui.action_Close)

Example #5 - Alternative

def showCCPL(self): with open('CCPL.txt') as fi: self.ui.textEdit.setText(fi.read())

def about(self): QMessageBox.about(self, "About PySide, Platform and the like" """<b> about.py version %s </b> <p>Copyright © 2013 by Algis Kabaila. This work is made available under the terms of Creative Commons Attribution-ShareAlike 3.0 license, http://creativecommons.org/licenses/by-sa/3.0/. <p>This application is useful for displaying Qt version and other details. <p>Python %s - PySide version %s - Qt version %s on %s""" % (__version__, platform.python_version(), PySide.__version__, PySide.QtCore.__version__, platform.system())) #------------------------------------------------------if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() sys.exit(app.exec_())

18 / 20

Page 19: Python GUI  PySide

References1. PySide-Newbie-Tutorials2. OldAl/tuts4pyside3. techtonik/pyside

Other Readings1. PySide - Qt Wiki2. PySide Tutorials - Qt Wiki3. PySide Example Applications4. PySide/Examples5. shuge/Qt-Python-Binding-Examples6. Friz-zy/basis

19 / 20

Page 20: Python GUI  PySide

ENDEueung Mulyana

http://eueung.github.io/python/pysidePython CodeLabs | Attribution-ShareAlike CC BY-SA

20 / 20