Python 如何将 .ui 文件转换为 .py 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22266802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:37:27  来源:igfitidea点击:

How to convert a .ui file to .py file

pythonuser-interfacepyqtqt-designer

提问by SonicFancy

This .ui file is made by Qt Designer. It's just a simple UI.

这个 .ui 文件是由 Qt Designer 制作的。这只是一个简单的用户界面。

All the commands or codes for doing this on the websites I have looked through are not for windows.

在我浏览过的网站上执行此操作的所有命令或代码都不适用于 Windows。

采纳答案by ekhumoro

The pyuic tool works in exactly the same way on all platforms:

pyuic 工具在所有平台上的工作方式完全相同:

C:\>pyuic4 -h
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]

I suspect the reason "it doesn't work" is that the .ui file you are trying to convert is not in the current directory. So you need to cd to that directory first:

我怀疑“它不起作用”的原因是您尝试转换的 .ui 文件不在当前目录中。所以你需要先 cd 到那个目录:

    C:\>cd C:\path\to\my\ui\files

then run pyuic:

然后运行pyuic:

    C:\path\to\my\ui\files\>pyuic4 -o ui_form.py form.ui

回答by Ayser

to convert from .ui to .py in windows

在 Windows 中从 .ui 转换为 .py

  1. go to the directory where your ui file in.
  2. press shift and mouse right click.
  3. click (open command window here.
  4. this will open the cmd. check what is the directory of your (pyuic4.bat) file. usually will be: C:\Python34\Lib\site-packages\PyQt4\pyuic4.bat.
  5. write in the cmd:
    C:\Python34\Lib\site-packages\PyQt4\pyuic4.bat -x filename.ui -o filename.py (hit Enter)
    this will generate a new file .py for your .ui file and in the same directory
  1. 转到您的 ui 文件所在的目录。
  2. 按 shift 并单击鼠标右键。
  3. 单击(在此处打​​开命令窗口。
  4. 这将打开cmd。检查您的(pyuic4.bat)文件的目录是什么。通常是:C:\Python34\Lib\site-packages\PyQt4\pyuic4.bat。
  5. 在 cmd 中写入:
    C:\Python34\Lib\site-packages\PyQt4\pyuic4.bat -x filename.ui -o filename.py(按 Enter)
    这将为您的 .ui 文件和在同一个目录

Note: this command for python 3.4 version and PyQt4 version. if you are using other versions you should change the numbers (e.g PyQt5)

注意:此命令适用于 python 3.4 版本和 PyQt4 版本。如果您使用其他版本,您应该更改数字(例如 PyQt5)

回答by Amr Sohil

Better Late Than Never, create a batch file on windows (.bat) and paste the following into it , save & run from the same directory as your files.

迟到总比没有好,在 Windows (.bat) 上创建一个批处理文件并将以下内容粘贴到其中,保存并从与文件相同的目录运行。

@echo off
title .UI to .py files converter !
echo Generate Python files from .UI files!
pause
echo ""
echo ""
echo ""
echo ""
echo UI file Name
set /p UiName=Enter .UI file Name: 
echo ""
echo ""
echo ""
echo ""
echo PY file Name
set /p PyName=Enter .PY file Name: 
echo ""
echo ""
echo ""
echo Start Converting Files Please wait.



call python -m PyQt5.uic.pyuic -x "%UiName%" -o "%PyName%"

echo QRC file Name
set /p QrName=Enter .qrc file Name: 
echo ""
echo ""
echo ""
echo ""
echo PY file Name
set /p PiName=Enter .PY file Name: 
echo ""
echo ""
echo ""
echo Start Converting Files Please wait.

pyrcc5 -o "%PiName%" "%QrName%"

echo Job Completed.
pause

回答by Amin habibi

In pyqt5 you can use: 1. convert to none-executable python file :

在 pyqt5 中,您可以使用: 1. 转换为不可执行的 python 文件:

pyuic5 -o pyfilename.py design.ui

2. convert to executable python file :

2.转换为可执行的python文件:

pyuic5 -x -o pyfilename.py design.ui

and also for resource diles(qrc): 1. convert qrcto python file :

以及资源 diles(qrc): 1. 转换qrc为 python 文件:

pyrcc5 -o pyfilename.py res.qrc

Note:that if you run the command in the wrong way,your uifile will be lost.

Note:如果您以错误的方式运行命令,您的ui文件将丢失。