lundi 24 juin 2013

Create windows GUI executable using Python & QtCreator & PyQt & cx_Freeze

Requirements

- QtCreator
- Python 3.3
- PyQt 4.1 (pyuic4 under linux / pyuic4.bat under windows)
- cx_Freeze 4.3.1 for Python 3.3


Step-by-step for a HelloWorld.exe


Step1: Create the ui form with a pushbutton named: btnHelloWorld


Step 2: Convert .ui to .py
Under windows:  pyuic4.bat -o mainwindow.py mainwindow.ui
Under linux:

Step 3: Implement Handlers.
Now create a helloworld.py file with following content:
import re   #Strange, but I do need this import to make the executable able to find QtCore
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mainwindow import *

class Handlers(object):   
    def btnHelloWorld_Clicked(self):       
        if QMessageBox.question(None, '', "HelloWorld, would you say goodbye ?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) == QMessageBox.Yes:
            sys.exit(app.exec_())

    def installHandlers(self, uimw):
        self.uimw = uimw
        uimw.MainWindow.connect(uimw.btnHelloWorld, SIGNAL("clicked()"), self.btnHelloWorld_Clicked)       
               
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("QPlastiqueStyle"))

    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.MainWindow = MainWindow

    handlers = Handlers()
    handlers.installHandlers(ui)

    MainWindow.show()
    sys.exit(app.exec_())
 Step 4: Create setup.py file with following content
 from cx_Freeze import setup, Executable
setup(
    name = "HelloWorld",
    version = "0.1",
    description = "PyQt4 Hellworld",
    executables = [Executable("helloworld.py")]
    )
Step 5: Open a command line, change to the source directory (setup.py, helloworld.py, mainwindow.py) and issue the command to compile the executable:
python.exe setup.py build

Step 6: Now the exefile can be found in the build directory, launch the exe
It works like a charm !!!




Aucun commentaire:

Enregistrer un commentaire