python gui programming with pyside - meetupfiles.meetup.com/2179791/python gui programming with...

25
Python GUI programming with PySide Speaker: BigLittle Date: 2013/03/04

Upload: lamminh

Post on 06-Aug-2018

307 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Python GUI programming with PySide

Speaker: BigLittle

Date: 2013/03/04

Page 2: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

CLI vs. GUI

CLI (Command Line Interface)

• Take less resources.

• User have much more control of their system.

• Only need to execute few line to perform a task.

GUI (Graphical User Interface)

• Easier for user to view and control your application.

• Ability of multitasking.

Page 3: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

About Python + GUI

• Some GUI library for Python:

• Tkinter - Python's standard GUI package. (Tcl/Tk)

• wxPython - A Python extension module that wraps wxWidgets library.

• PyQt4 - A Python binding of the cross-platform GUI toolkit Qt.

• PySide - A Python binding of the cross-platform GUI toolkit Qt.

• Qt & wxWidgets are both well known C++ GUI library:

• They are free and open source.

• They are cross-platform and support Unicode.

• They have full documentation and community support.

Page 4: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Why Qt / PySide?

• Pros:

• It’s NOT MFC style.

• Easy to build up by QtDesigner or text coding.

• Intuitive SIGNAL / SLOT mechanism.

• Supports I18N. (Internationalization)

• LGPL. (GNU Lesser General Public License)

• The API is more Pythonic.

• Cons:

• Qt is not written in standard C++ library.

• Application size is big.

• blabla…… I don’t care, and you?

Page 5: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Before Starting…

• Install Python (What?!)

• http://www.python.org/download

• User should note the compatibility between version 2.7.x and 3.x.

• Install Qt

• http://qt-project.org/downloads

• The latest version is 5.0. (It’s too new for me... =.=)

• Install PySide

• http://qt-project.org/wiki/PySideDownloads

• Current version is 1.1.2, but most references on web is for 1.0.7.

Page 6: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

A Simple Program

• test.py

Just so easy!

object oriented programming styles

Page 7: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

A Simple Program (cont’d)

• test.py

Import PySide module here.

Page 8: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

A Simple Program (cont’d)

• test.py

Inherit class “QMainWindow”

set it’s title then show itself.

Page 9: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

A Simple Program (cont’d)

• test.py

Create MyWindow instance

Contains the main event loop.

Page 10: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

A Simple Program (cont’d)

• test.py

Trigger when execute test.py.

Actually, the easiest way may be: app = QApplication(sys.argv) main = QMainWindow() main.setWindowTitle(“First GUI”) main.show() sys.exit(app.exec_())

Page 11: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Where Is Other Stuff?

• Just like a HD, you must format it then you can put in your data.

• HD Current Window

• Format QLayout

• Data QWidget

• QLayout and QWidget are the most basic classes that make up your GUI.

QMainWindow is a subclass of QWidget. Right figure shows the default configuration.

Page 12: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

QLayout

• In fact, you don’t need to start from QLayout itself. There are many pre-designed layout. Use them directly should be enough.

QHBoxLayout

QVBoxLayout

QGridLayout

QFormLayout

Page 13: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

QWidget

• Example Think: What’s the layout setting

in the QGroupBox?

Page 14: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Let’s Put Something On It

Page 15: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

How To Activate It?

• Now you have a “skin” of your application. Let’s make it activated. For example, push “Big” button then type “Bigger” on the editor.

• In Qt, trigger & response are called SIGNAL & SLOT

trigger response

Page 16: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

SIGNAL / SLOT

• self.btn1.clicked.connect(self.typebig)

• The advantage of SIGNAL / SLOT is the caller does not have to know anything about the receiver and vice versa.

• Any function could be a SLOT.

• One can connect several SLOT to a SIGNAL and vice versa.

• A common usage: Real-time check for input contents

Page 17: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Exercise…

• Try to modify the simple program to MD5 Encrypter.

Page 18: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Exercise… (cont’d)

• You need import hashlib first

Page 19: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Use Advanced Module

• Matplotlib - A Python plotting library. (Based on PySide!)

• SciPy - An open source library of scientific tools for Python.

• VTK - Visual Tool Kit (TVTK, Mayavi)

Page 20: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Much Better Than Excel…

• Source code: http://matplotlib.org/examples/pylab_examples/demo_agg_filter.html

Page 21: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Something To Be Continued

• Decorations:

• Take advantages of groups, tabs, tooltips, …, etc.

• Add some icons, sounds or visual effects.

• Maintains:

• Debug, debug and debug.

• Packing/Release:

• PyInstaller - Cross platform

• cx_Freeze - Cross platform

• py2exe - Window only

CurveExpert (PyInstaller)

BitTorrent (py2exe)

Page 22: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Create Your Own GUI App!

• Model-View-Controller

• Automation Tool

• Distributed Programming

• Database Management

• Multimedia

• CAD Software

• Embedded System

• Web Application

• Games

• ……

Page 23: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Summary

• GUI programming could help you interactively control the application and report visualized final data. Every programmer should familiar with its basic concept.

• Programming in PySide = Programming in OOP. All components in library can be easily adapted and modified. Try to use them as much as possible.

• There are many “splendid” functions like graphics, animation, drag & drop…, etc. for GUI programming. You can improve the user experience by these, but remember to pay more attention on the performance of your application.

Page 25: Python GUI programming with PySide - Meetupfiles.meetup.com/2179791/Python GUI programming with PySide.pdf · (Tcl/Tk) •wxPython - A Python extension module that wraps wxWidgets

Thanks for your attention.