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

问题描述 投票:11回答:3

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

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

python user-interface pyqt qt-designer
3个回答
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]

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

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

然后运行pyuic:

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

10
投票

在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)


1
投票

Better Late Than Never,在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
© www.soinside.com 2019 - 2024. All rights reserved.