How to save a form from Qt Designer as a standalone app?

前端 未结 1 1740
醉话见心
醉话见心 2021-01-15 14:38

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

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 15:07

    To create a standalone app with PyInstaller follow these steps:

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

      
      
       MainWindow
       
        
         
          0
          0
          147
          125
         
        
        
         MainWindow
        
        
         
          
           
          
          
           
            
             Click Me
            
           
          
         
        
        
         
          
           0
           0
           147
           25
          
         
         
          
           Menu
          
         
         
        
        
       
       
       
      
      
    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.

    0 讨论(0)
提交回复
热议问题