3

I have created a mock-up of a form in Qt Designer, and now I would like to save the form as a (exe?) file so that it can be run on the computer.

Would I use 'Python to Exe' (if so, how)? I don't know much about programming yet.

Qt Designer saves the files with a .ui extension.

  • 1
    http://qt-project.org/forums/viewthread/4535 – CppLearner Dec 27 '12 at 08:31
  • I also found this, but not sure how to make the .ui a .py for Pyinstaller http://stackoverflow.com/questions/5888870/how-do-i-compile-a-pyqt-script-py-to-a-single-standalone-executable-file-for – Duh Compewtuhr Dec 27 '12 at 09:14
  • you can't. You need to code. Ui is just the front end. There is no logic. Code it. – CppLearner Dec 27 '12 at 09:15
  • Couldn't the exe just be nonfunctional buttons for now? I was hoping that a mock-up standalone GUI form could help me to learn Python (file processing, etc.) Here are some more links to similar questions. I guess this is becoming a popular subject! http://stackoverflow.com/questions/602076/how-to-build-pyqt-project http://stackoverflow.com/questions/8548904/pyinstaller-error-with-pyqt-when-trying-to-build-onefile https://groups.google.com/forum/#!topic/pyinstaller/vtbKKt6v0is – Duh Compewtuhr Dec 27 '12 at 10:00
  • https://groups.google.com/forum/?fromgroups=#!topic/pyinstaller/V4z54vuKCu0 http://lateral.netmanagers.com.ar/weblog/posts/BB955.html http://stackoverflow.com/questions/1737375/building-executables-for-python-3-and-pyqt http://thewikiblog.appspot.com/wiki/be-productive-with-pyqt4 Someone who understands Python better than I could probably figure it out from these links, sadly, I am still a novice. – Duh Compewtuhr Dec 27 '12 at 10:00
  • Also you can render ui files without compiling with pyuic. http://stackoverflow.com/questions/2398800/linking-a-qtdesigner-ui-file-to-python-pyqt – MGP Dec 27 '12 at 15:30
  • What do you mean by 'render'? Would I be running it in the interpreter, instead of as an independent app? That might work for now, while I am still developing it... – Duh Compewtuhr Dec 27 '12 at 19:51
  • 2
    You should run that command in you OS command-line shell (CMD.EXE), not in the python shell –  Dec 27 '12 at 20:57
  • Ah. =D I've got the Ui_MyWidget.py created & now I am working through the main part. – Duh Compewtuhr Dec 27 '12 at 21:07

1 Answers1

3

To create a standalone app with PyInstaller follow these steps:

  1. Save this code as your MyWidget.ui file:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>147</width>
        <height>125</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QLineEdit" name="lineEdit"/>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>Click Me</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>147</width>
         <height>25</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuMenu">
        <property name="title">
         <string>Menu</string>
        </property>
       </widget>
       <addaction name="menuMenu"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
  2. Compile your MyWidget.ui file into Ui_MyWidget.py using pyuic4 with this command from your OS shell command-line:

    pyuic4 "/path/to/MyWidget.ui" -o "Ui_MyWidget.py"
    

    This command will create a Ui_MyWidget.py file in your current directory with this contents:

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'MyWidget.ui'
    #
    # Created: Fri Dec 28 03:45:13 2012
    #      by: PyQt4 UI code generator 4.7.3
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt4 import QtCore, QtGui
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(147, 125)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
            self.verticalLayout.setObjectName("verticalLayout")
            self.lineEdit = QtGui.QLineEdit(self.centralwidget)
            self.lineEdit.setObjectName("lineEdit")
            self.verticalLayout.addWidget(self.lineEdit)
            self.pushButton = QtGui.QPushButton(self.centralwidget)
            self.pushButton.setObjectName("pushButton")
            self.verticalLayout.addWidget(self.pushButton)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 147, 25))
            self.menubar.setObjectName("menubar")
            self.menuMenu = QtGui.QMenu(self.menubar)
            self.menuMenu.setObjectName("menuMenu")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.menubar.addAction(self.menuMenu.menuAction())
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
            self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Click Me", None, QtGui.QApplication.UnicodeUTF8))
            self.menuMenu.setTitle(QtGui.QApplication.translate("MainWindow", "Menu", None, QtGui.QApplication.UnicodeUTF8))
    
  3. Save this code as your MyWidget.py file:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtCore, QtGui
    from Ui_MyWidget import Ui_MainWindow
    
    class MyWidget(QtGui.QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
           super(MyWidget, self).__init__(parent)
    
           self.setupUi(self)
    
        @QtCore.pyqtSlot()
        def on_pushButton_clicked(self):
            self.lineEdit.setText("A Qt standalone app!")
    
    if __name__ == '__main__':
        import sys
    
        app = QtGui.QApplication(sys.argv)
        window = MyWidget()
        window.resize(300, 30)
        window.show()
        sys.exit(app.exec_())
    
  4. Check that you can run MyWidget.py without errors (MyWidget.py and Ui_MyWidget.py need to be in the same folder), and once done configuring PyInstaller (checkout the README file) from your OS shell command-line cd into the pyinstaller directory and run this command:

    python pyinstaller.py --onefile '/path/to/MyWidget.py'
    
  5. Look for a folder called MyWidget in the pyinstaller folder, inside the dist folder is your standalone Qt app.

  • Awesome! ...Not sure if it works yet, I keep getting syntax errors on the first part. I've tried putting the complete path (C:\python27\etcetc) for each part. The error seems to be in the "file_input" part. – Duh Compewtuhr Dec 27 '12 at 19:45
  • @DuhCompewtuhr Could you update your post with the code you are using? Please consider upvoting and/or accepting my answer if it was of any help –  Dec 27 '12 at 19:55
  • I am very excited to have this question answered, but I haven't been able to check it yet, because I am such a novice :( Here is an example of the type of error that I have been getting. I've tried many different combinations. >>> C:\Python27\Lib\site-packages\PyQt4\pyuic4.bat C:\MyWidget.ui -o C:\Ui_MyWidget.py File "", line 1 C:\Python27\Lib\site-packages\PyQt4\pyuic4.bat C:\MyWidget.ui -o C:\Ui_MyWid get.py ^ SyntaxError: invalid syntax >>> (Sorry, I can't seem to get the formatting to work with these comments) – Duh Compewtuhr Dec 27 '12 at 20:08
  • @DuhCompewtuhr It's pretty difficult to see what's going on there, please edit your original post with that code to get the proper formatting –  Dec 27 '12 at 20:16
  • Okay, I have edited the original post. I'm not sure what I am doing wrong in the command prompt. – Duh Compewtuhr Dec 27 '12 at 20:41
  • when I replace Ui_Form with the name of my actual class, and replace QMainWindow, should I look somewhere for the names of those (variables?) or just make them up? – Duh Compewtuhr Dec 27 '12 at 21:18
  • Looks like I've almost got it! Everything seems to be working until it gets to a syntax error (on line 6 of MyWidget.py) because I haven't properly replaced Ui_Form with the name of my actual class, and I haven't replaced QMainWindow with the name of the class of my actual widget... Not sure what I should name them? – Duh Compewtuhr Dec 27 '12 at 21:57
  • @DuhCompewtuhr Could you post or link to a version of your code? Checkout this [site](http://tny.cz/) –  Dec 27 '12 at 22:15
  • I need to see the contents of your Widget.ui file, upload that –  Dec 27 '12 at 22:24
  • Okay, here is the extremely simplified form that I have been using to make it easier to figure all this out: http://tny.cz/86c909a9 – Duh Compewtuhr Dec 27 '12 at 22:33
  • @DuhCompewtuhr checkout my [updated post](http://stackoverflow.com/a/14053207/1006989), I added some information to describe the steps necessary to create a standalone Qt app –  Dec 28 '12 at 03:17
  • Hello, World! It works!! Thank you so much for all your help! I have taken a few notes for posterity, based on your numbered outline. 1.) I believe that this code would be generated from PyQt or Qt Designer: Must the widget be of the class "QMainWindow' to become a standalone app? 2.) 2A.) I dragged & dropped the files into the command line because I haven't yet figured out the (PATH?) configurations (for the command line). 2B.) I believe the example provided would be different if the original .ui file were changed. 2C.) note: pyuic4 converts the .ui file from Qt Designer to a .py file... – Duh Compewtuhr Dec 28 '12 at 04:44
  • 3.) Would the code provided in step 3 change at all if the original .ui file were different? 4.) 4A.) I double-clicked on the MyWidget.py file to check it & it created "Ui_MyWidget.pyc" in the main directory & a partial MyWidget folder in the pyinstaller folder (without the dist folder). 4B.) the README file was in an unknown ".rst" format, but everything worked fine anyway. 4C.) After typing everything in the command line, the dist folder was created with the STANDALONE FORM APP just as you said! Thank you so much for all your help! Muchas Gracias! – Duh Compewtuhr Dec 28 '12 at 04:51