我在Qt Designer中创建了一个表单的模型,现在我想将表单保存为(exe?)文件,以便它可以在计算机上运行。
我会使用'Python to Exe'(如果是这样,如何)?我对编程还不太了解。
Qt Designer使用.ui扩展名保存文件。
答案 0 :(得分:2)
要使用 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>
使用 pyuic4 将 MyWidget.ui 文件编译到 ui_MyWidget.py ,并使用此命令从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_())
检查您是否可以无误地运行 MyWidget.py ( MyWidget.py 和 Ui_MyWidget.py 需要在同一个文件夹),一旦完成从操作系统shell命令行cd配置 PyInstaller (签出自述文件)到 pyinstaller 目录并运行此命令:
python pyinstaller.py --onefile '/path/to/MyWidget.py'
在 pyinstaller 文件夹中查找名为 MyWidget 的文件夹, dist 文件夹中的文件是您的独立Qt应用程序。