将pyQt UI转换为python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18429452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert pyQt UI to python
提问by Shannon Hochkins
Is there a way to convert a ui formed with qtDesigner to a python version to use without having an extra file?
有没有办法将使用 qtDesigner 形成的 ui 转换为 python 版本以使用而无需额外文件?
I'm using Maya for this UI, and converting this UI file to a readable python version to implement would be really great!
我正在为这个 UI 使用 Maya,将这个 UI 文件转换为可读的 Python 版本来实现会非常棒!
采纳答案by Katsu
You can use pyuic4
command on shell:
pyuic4 input.ui -o output.py
您可以pyuic4
在 shell 上使用命令:
pyuic4 input.ui -o output.py
回答by Pouya
I'm not sure if PyQt does have a script like this, but after you install PySide there is a script in pythons script directory "uic.py". You can use this script to convert a .ui file to a .py file:
我不确定 PyQt 是否有这样的脚本,但是在您安装 PySide 之后,pythons 脚本目录“uic.py”中有一个脚本。您可以使用此脚本将 .ui 文件转换为 .py 文件:
python uic.py input.ui -o output.py -x
回答by hoylpraal
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:
如果您使用的是 windows,默认情况下 PyQt4 文件夹不在路径中,您必须在尝试运行之前转到它:
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
回答by Riccardo
I've ran into the same problem recently. After finding the correct path to the pyuic4 file using the file finder I've ran:
我最近遇到了同样的问题。使用文件查找器找到 pyuic4 文件的正确路径后,我运行了:
C:\Users\ricckli.qgis2\python\plugins\qgis2leaf>C:\OSGeo4W64\bin\pyuic4 -o ui_q gis2leaf.py ui_qgis2leaf.ui
C:\Users\ricckli.qgis2\python\plugins\qgis2leaf>C:\OSGeo4W64\bin\pyuic4 -o ui_q gis2leaf.py ui_qgis2leaf.ui
As you can see my ui file was placed in this folder...
如您所见,我的 ui 文件已放置在此文件夹中...
QT Creator was installed separately and the pyuic4 file was placed there with the OSGEO4W installer
QT Creator 是单独安装的,pyuic4 文件与 OSGEO4W 安装程序一起放在那里
回答by MagSec
You don't have to install PyQt4 with all its side features, you just need the PyQt4 package itself. Inside the package you could use the module pyuic.py ("C:\Python27\Lib\site-packages\PyQt4\uic") to convert your Ui file.
您不必安装 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
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py -help
update python3: use pyuic5 -help
# add filepath if needed. pyuic version = 4 or 5.
更新 python3:pyuic5 -help
如果需要,使用# 添加文件路径。pyuic 版本 = 4 或 5。
You will get all options listed:
您将获得列出的所有选项:
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]
So your command will look like this:
所以你的命令看起来像这样:
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py test_dialog.ui -o test.py -x
You could also use full file paths to your file to convert it.
您还可以使用文件的完整文件路径来转换它。
Why do you want to convert it anyway? I prefer creating widgets in the designer and implement them with via the *.ui file. That makes it much more comfortable to edit it later. You could also write your own widget plugins and load them into the Qt Designer with full access. Having your ui hard coded doesn't makes it very flexible.
你为什么要转换它呢?我更喜欢在设计器中创建小部件并通过 *.ui 文件实现它们。这使得以后编辑它变得更加舒适。您还可以编写自己的小部件插件并将它们加载到具有完全访问权限的 Qt 设计器中。对 ui 进行硬编码并不会使其非常灵活。
I reuse a lot of my ui's not only in Maya, also for Max, Nuke, etc.. If you have to change something software specific, you should try to inherit the class (with the parented ui file) from a more global point of view and patch or override the methods you have to adjust. That saves a lot of work time. Let me know if you have more questions about it.
我不仅在 Maya 中重用了很多 ui,还用于 Max、Nuke 等。如果您必须更改特定于软件的内容,您应该尝试从更全局的查看和修补或覆盖您必须调整的方法。这样可以节省大量的工作时间。如果您对此有更多疑问,请告诉我。
回答by chris mahn
Quickest way to convert .ui to .py is from terminal:
将 .ui 转换为 .py 的最快方法是从终端:
pyuic4 -x input.ui -o output.py
Make sure you have pyqt4-dev-tools installed.
确保安装了 pyqt4-dev-tools。
回答by aoh
The question has already been answered, but if you are looking for a shortcut during development, including this at the top of your python script will save you some time but mostly let you forget about actually having to make the conversion.
问题已经得到解答,但如果您在开发过程中寻找捷径,将其包含在 Python 脚本的顶部会为您节省一些时间,但主要是让您忘记实际上必须进行转换。
import os #Used in Testing Script
os.system("pyuic4 -o outputFile.py inpuiFile.ui")
回答by Awinash jaiswal
For pyqt5 you can use
对于 pyqt5,您可以使用
pyuic5 xyz.ui > xyz.py
or
或者
pyuic5 xyz.ui -o xyz.py
回答by Rick M.
Update for anyone using PyQt5 with python 3.x:
使用 PyQt5 和 python 3.x 的任何人的更新:
- Open terminal (eg. Powershell, cmd etc.)
- cd into the folder with your
.ui
file. - Type:
"C:\python\Lib\site-packages\PyQt5\pyuic5.bat" -x Trial.ui -o trial_gui.py
for cases where PyQt5 is not a path variable. The path in quotes " " represents where thepyuic5.bat
file is.
- 打开终端(例如 Powershell、cmd 等)
- cd 到您的
.ui
文件所在的文件夹中。 - 类型:
"C:\python\Lib\site-packages\PyQt5\pyuic5.bat" -x Trial.ui -o trial_gui.py
用于 PyQt5 不是路径变量的情况。引号 " " 中的路径表示pyuic5.bat
文件所在的位置。
This should work!
这应该有效!
回答by Kandy
For Ubuntu it works for following commands; If you want individual files to contain main method to run the files individually, may be for testing purpose,
对于 Ubuntu,它适用于以下命令;如果您希望单个文件包含单独运行文件的主要方法,可能用于测试目的,
pyuic5 filename.ui -o filename.py -x
No main method in file, cannot run individually... try
文件中没有主要方法,无法单独运行...试试
pyuic5 filename.ui -o filename.py
Consider, I'm using PyQT5.
考虑一下,我正在使用 PyQT5。