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

Son*_*ncy 13 python user-interface pyqt qt-designer

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

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

ekh*_*oro 16

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]
Run Code Online (Sandbox Code Playgroud)

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

    C:\>cd C:\path\to\my\ui\files
Run Code Online (Sandbox Code Playgroud)

然后运行pyuic:

    C:\path\to\my\ui\files\>pyuic4 -o ui_form.py form.ui
Run Code Online (Sandbox Code Playgroud)


Ays*_*ser 11

在Windows中将.ui转换为.py

  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文件生成一个新文件.py同一目录

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


Ami*_*ibi 11

在 pyqt5 中,您可以使用:

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

    pyuic5 -o pyfilename.py design.ui

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

    pyuic5 -x -o pyfilename.py design.ui

以及资源 diles(qrc):

  1. 转换qrc为python文件:

    pyrcc5 -o pyfilename.py res.qrc

Note:如果你以错误的方式运行命令,你的ui文件将会丢失。所以你必须复制你的文件:)


小智 7

迟到总比不到好,在 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
Run Code Online (Sandbox Code Playgroud)