我已经在Qt Designer中创建了一个窗体的模型,现在我想将窗体保存为(exe?)文件,以便它可以在计算机上运行。
我会使用"Python到Exe"(如果是,如何)?我还不太懂编程。
Qt Designer以。ui扩展名保存文件。
要使用PyInstaller创建一个独立的应用程序,请遵循以下步骤:
-
将此代码保存为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>
-
编译你的MyWidgetui文件到Ui_MyWidget.py使用pyuic4在OS shell命令行中使用以下命令:
pyuic4 "/path/to/MyWidget.ui" -o "Ui_MyWidget.py"
此命令将在当前目录中创建一个Ui_MyWidget.py文件,内容如下:
# -*- 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_())
-
检查你可以运行 mywidgets .py没有错误( mywidgets .py和 ui_mywidgets .py需要在同一个文件夹中),并且一旦完成配置PyInstaller(签出README文件)从你的OS shell命令行cd到 PyInstaller目录并运行这个命令:
python pyinstaller.py --onefile '/path/to/MyWidget.py'
-
在pyinstaller文件夹中查找名为MyWidget的文件夹,在dist文件夹中是您的独立Qt应用程序。