如何将 Qt Designer 中的表单另存为独立应用程序? [英] How to save a form from Qt Designer as a standalone app?

查看:61
本文介绍了如何将 Qt Designer 中的表单另存为独立应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt Designer 中创建了一个表单模型,现在我想将表单保存为(exe?)文件,以便它可以在计算机上运行.

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.

我会使用Python to Exe"吗(如果是,如何使用)?我对编程还不太了解.

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

Qt Designer 使用 .ui 扩展名保存文件.

Qt Designer saves the files with a .ui extension.

推荐答案

要使用 PyInstaller 创建独立应用程序,请按照下列步骤操作:

To create a standalone app with PyInstaller follow these steps:

  1. 将此代码保存为您的 MyWidget.ui 文件:

<?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>

  • 使用 pyuic4 将您的 MyWidget.ui 文件编译到 Ui_MyWidget.py 中,并在您的 OS shell 命令行中使用以下命令:

  • 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"
    

    此命令将在您的当前目录中创建一个 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))
    

  • 将此代码保存为您的 MyWidget.py 文件:

    #!/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_())
    

  • 检查您是否可以无错误地运行 MyWidget.py(MyWidget.pyUi_MyWidget.py 需要在同一个文件夹),一旦完成配置 PyInstaller(检查自述文件)从你的 OS shell 命令行 cd 进入 pyinstaller 目录并运行这个命令:

  • 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'
    

  • pyinstaller 文件夹中查找名为 MyWidget 的文件夹,dist 文件夹内是您的独立 Qt 应用.

  • Look for a folder called MyWidget in the pyinstaller folder, inside the dist folder is your standalone Qt app.

    这篇关于如何将 Qt Designer 中的表单另存为独立应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆