Python GUI and Packing

With PySide and cx_freeze

This can be done in four steps:

  1. Create GUI
  2. Convert ui-file to py-file
  3. Implement GUI
  4. Packing for Windoze desktop-app

Let's beak it down

Create a Python app.

c:\tutorial\App.py

Create python package named "ui" (This is a folder with an empty __init__.py file).

c:\tutorial\ui\__init__.py

1. Create GUI

Start QtDesigner and create "Dialog without Buttons". Drag and drop a PushButton and Label into the Dialog. Save QtDesigner ui-file

c:\tutorial\ui\app_gui.ui

2. Convert QtDesign to Python

Convert design-file to Python-file with

c:\python34\script\pyside-uic -o c:\tutorial\ui\app_gui.py c:\tutorial\ui\app_gui.py

3. Implement user interface to your app

c:\tutorial\App.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
import sys
from datetime import date
from PySide.QtGui import *
from ui.app_gui import Ui_Dialog
version = date.strftime(date.today(), '%Y.%m.%d')


class App(QDialog, Ui_Dialog):
def __init__(self):
super(App, self).__init__()
self.setupUi(self)

if __name__ == '__main__':
app = QApplication(sys.argv)
form = App()
form.show()
sys.exit(app.exec_())

Now we can communicate with the widgets PushButton and Label (line 13-17).

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
import sys
from datetime import date
from PySide.QtGui import *
from ui.app_gui import Ui_Dialog
version = date.strftime(date.today(), '%Y.%m.%d')


class App(QDialog, Ui_Dialog):
def __init__(self):
super(App, self).__init__()
self.setupUi(self)

# Add text to Label
self.label.setText('This is the Label')

# Action for PushButton
self.pushButton.clicked.connect(sys.exit)

if __name__ == '__main__':
app = QApplication(sys.argv)
form = App()
form.show()
sys.exit(app.exec_())

4. Packing

We can use the following template

c:\tutorial\setup.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
import sys
from cx_Freeze import setup, Executable

sys.argv.append('build')
# Insert the application name
import App as Program

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": [],
'includes': ['atexit'],
'include_files': [],
'include_msvcr': True,
'zip_include_packages': '*',
'zip_exclude_packages': '',
'build_exe': '%s_%s' % ((Program.__name__, Program.version))}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = 'Console'
if sys.platform == "win32":
base = "Win32GUI"

setup(name=Program.__name__,
version=Program.version,
description='App with GUI packed with cx_freeze',
options={"build_exe": build_exe_options},
executables=[Executable("%s.py" % Program.__name__, base=base)])

Run c:\tutorial\setup.py There will be a folder with your App.exe located in a sub directory.

title

Program versions in this tutorial: