GUIs in Python

More information at http://www.pyside.org.

PySide is a Python binding of the cross-platform GUI toolkit Qt. It is one of the alternatives to the standard library package Tkinter. Like Qt, PySide is free software.

Simple example

In [ ]:
from PySide.QtCore import *
from PySide.QtGui import *

try:
    app = QApplication([])
except RuntimeError:
    pass

class Form(QDialog):   
    def __init__(self):
        super(Form, self).__init__()
        self.resize(320,200)
        # Create widgets
        self.edit = QLineEdit("Write my name here")
        self.button = QPushButton("Show Greeting")        
        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.edit)
        layout.addWidget(self.button)
        # Set dialog layout
        self.setLayout(layout)
        # Add button signal to greetings slot
        self.button.clicked.connect(self.greetings)
       
    # Greets the user
    def greetings(self):
        print ("Hello {}".format(self.edit.text()))
 
 
form = Form()
form.show()

app.exec_()

Using the GUI designer

First, use the Designer program to design the GUI:

In [ ]:
# Debian/Ubuntu: apt-get install pyside-tools qt4-designer / OSX: sudo port install py-pyside-tools
!Designer gui.ui
!cat gui.ui

Then compile the .ui file to a python .py file:

In [ ]:
!pyside-uic-2.7 gui.ui -o gui.py
!cat gui.py

Finally, use a little wrapper program to run the GUI:

In [ ]:
!cat main.py
In [ ]:
!python main.py