有没有办法将使用 qtDesigner 形成的 ui 转换为 python 版本以便在没有额外文件的情况下使用?
我正在为这个 UI 使用 Maya,并将这个 UI 文件转换为可读的 python 版本来实现真的很棒!
您可以pyuic4
在 shell 上使用命令:
pyuic4 input.ui -o output.py
对于 pyqt5,您可以使用
pyuic5 xyz.ui > xyz.py
或者
pyuic5 xyz.ui -o xyz.py
If you are using windows, the PyQt4 folder is not in the path by default, you have to go to it before trying to run it:
c:\Python27\Lib\site-packages\PyQt4\something> pyuic4.exe full/path/to/input.ui -o full/path/to/output.py
or call it using its full path
full/path/to/my/files> c:\Python27\Lib\site-packages\PyQt4\something\pyuic4.exe input.ui -o output.py
这个问题已经得到解答,但是如果您在开发过程中寻找捷径,在您的 python 脚本顶部包含此捷径将节省您一些时间,但主要是让您忘记实际必须进行转换。
import os #Used in Testing Script
os.system("pyuic4 -o outputFile.py inpuiFile.ui")
我不确定 PyQt 是否有这样的脚本,但是在安装 PySide 后,pythons 脚本目录“uic.py”中有一个脚本。您可以使用此脚本将 .ui 文件转换为 .py 文件:
python uic.py input.ui -o output.py -x
将 .ui 转换为 .py 的最快方法是从终端:
pyuic4 -x input.ui -o output.py
确保您已安装 pyqt4-dev-tools。
您不必安装 PyQt4 及其所有附带功能,您只需要 PyQt4 包本身。在包中,您可以使用模块 pyuic.py ("C:\Python27\Lib\site-packages\PyQt4\uic") 来转换您的 Ui 文件。
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py -help
更新 python3:pyuic5 -help
如果需要,使用 # 添加文件路径。pyuic 版本 = 4 或 5。
您将列出所有选项:
Usage: pyuic4 [options] <ui-file>
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p, --preview show a preview of the UI instead of generating code
-o FILE, --output=FILE
write generated code to FILE instead of stdout
-x, --execute generate extra code to test and display the class
-d, --debug show debug output
-i N, --indent=N set indent width to N spaces, tab if N is 0 [default:
4]
-w, --pyqt3-wrapper generate a PyQt v3 style wrapper
Code generation options:
--from-imports generate imports relative to '.'
--resource-suffix=SUFFIX
append SUFFIX to the basename of resource files
[default: _rc]
因此,您的命令将如下所示:
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py test_dialog.ui -o test.py -x
您还可以使用文件的完整文件路径进行转换。
你为什么要转换它呢?我更喜欢在设计器中创建小部件并通过 *.ui 文件实现它们。这使得以后编辑它更舒服。您还可以编写自己的小部件插件并将它们加载到具有完全访问权限的 Qt 设计器中。硬编码你的 ui 并不能使它非常灵活。
我不仅在 Maya 中重用了很多我的 ui,还用于 Max、Nuke 等。如果您必须更改特定于软件的某些内容,您应该尝试从更全局的角度继承类(使用父 ui 文件)查看和修补或覆盖您必须调整的方法。这样可以节省很多工作时间。如果您对此有更多疑问,请告诉我。
当我尝试将 UI 转换为 PY 时遇到了一些错误,最后我找到了这个解决方案。
首先,如果找不到pyuic5.bat
文件,请复制此代码并将其粘贴到您的 cmd:
C:\Users\Monster>cd Desktop
C:\Users\Monster\Desktop>python -m PyQt5.uic.pyuic -x trial.ui -o trial.py
而且问题很容易解决!
使用 PyQt5 和 python 3.x 的任何人的更新:
.ui
文件夹。"C:\python\Lib\site-packages\PyQt5\pyuic5.bat" -x Trial.ui -o trial_gui.py
适用于 PyQt5 不是路径变量的情况。引号中的路径“”表示pyuic5.bat
文件所在的位置。这应该工作!
在您拥有文件的目录中打开 cmd 并键入:
pyuic5 –x "filename".ui –o "filename".py
我最近遇到了同样的问题。使用文件查找器找到 pyuic4 文件的正确路径后,我运行了:
C:\Users\ricckli.qgis2\python\plugins\qgis2leaf>C:\OSGeo4W64\bin\pyuic4 -o ui_q gis2leaf.py ui_qgis2leaf.ui
如您所见,我的 ui 文件已放置在此文件夹中...
QT Creator 单独安装,pyuic4 文件与 OSGEO4W 安装程序一起放置在那里
对于 Ubuntu,它适用于以下命令;如果您希望单个文件包含单独运行文件的主要方法,可能是出于测试目的,
pyuic5 filename.ui -o filename.py -x
文件中没有主要方法,无法单独运行...尝试
pyuic5 filename.ui -o filename.py
考虑一下,我正在使用 PyQT5。