Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a .ui file to .py file

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

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

like image 350
SonicFancy Avatar asked Mar 08 '14 08:03

SonicFancy


People also ask

How do I import a PyQt5 .UI file into a python GUI?

First we need to import the modules required. We need QtWidgets from PyQt5 for the base widget and uic from PyQt5 also to load the file. We also need sys to access arguments. Next we need to create a base class that will load the .


4 Answers

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

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:

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

then run pyuic:

    C:\path\to\my\ui\files\>pyuic4 -o ui_form.py form.ui
like image 160
ekhumoro Avatar answered Oct 13 '22 16:10

ekhumoro


To convert from .ui to .py in Windows

  1. Go to the directory where your ui file is.
  2. Press shift right-click your mouse.
  3. Click open command window here.
  4. This will open the cmd, check what is the directory of your (pyuic4.bat) file. Usually, it is in: 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

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)

like image 24
Ayser Avatar answered Oct 13 '22 14:10

Ayser


In pyqt5 you can use:

  1. convert to none-executable python file :

    pyuic5 -o pyfilename.py design.ui

  2. convert to executable python file :

    pyuic5 -x -o pyfilename.py design.ui

and also for resource diles(qrc):

  1. convert qrc to python file :

    pyrcc5 -o pyfilename.py res.qrc

Note: that if you run the command in the wrong way,your ui file will be lost.So you have to make a copy of your files:)

like image 24
Peyman habibi Avatar answered Oct 13 '22 14:10

Peyman habibi


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.

@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
like image 27
Amr Sohil Avatar answered Oct 13 '22 16:10

Amr Sohil