Convert pyQt UI to python

Question:

Is there a way to convert a ui formed with qtDesigner to a python version to use without having an extra file?

I’m using Maya for this UI, and converting this UI file to a readable python version to implement would be really great!

Asked By: Shannon Hochkins

||

Answers:

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:

python uic.py input.ui -o output.py -x
Answered By: Pouya

You can use pyuic4 command on shell:
pyuic4 input.ui -o output.py

Answered By: Katsu

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:

c:Python27Libsite-packagesPyQt4something> 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:Python27Libsite-packagesPyQt4somethingpyuic4.exe input.ui -o output.py
Answered By: hoylpraal

I’ve ran into the same problem recently. After finding the correct path to the pyuic4 file using the file finder I’ve ran:

C:Usersricckli.qgis2pythonpluginsqgis2leaf>C:OSGeo4W64binpyuic4 -o ui_q gis2leaf.py ui_qgis2leaf.ui

As you can see my ui file was placed in this folder…

QT Creator was installed separately and the pyuic4 file was placed there with the OSGEO4W installer

Answered By: Riccardo

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:Python27Libsite-packagesPyQt4uic”) to convert your Ui file.

C:test>python C:Python27x64Libsite-packagesPyQt4uicpyuic.py -help

update python3: use pyuic5 -help # add filepath if needed. pyuic version = 4 or 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:Python27x64Libsite-packagesPyQt4uicpyuic.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.

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.

Answered By: MagSec

Quickest way to convert .ui to .py is from terminal:

pyuic4 -x input.ui -o output.py

Make sure you have pyqt4-dev-tools installed.

Answered By: chris mahn

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.

import os #Used in Testing Script
os.system("pyuic4 -o outputFile.py inpuiFile.ui")
Answered By: aoh

For pyqt5 you can use

pyuic5 xyz.ui > xyz.py 

or

pyuic5 xyz.ui -o xyz.py
Answered By: Awinash jaiswal

Update for anyone using PyQt5 with python 3.x:

  1. Open terminal (eg. Powershell, cmd etc.)
  2. cd into the folder with your .ui file.
  3. Type:
    "C:pythonLibsite-packagesPyQt5pyuic5.bat" -x Trial.ui -o trial_gui.py
    for cases where PyQt5 is not a path variable. The path in quotes ” ” represents where the pyuic5.bat file is.

This should work!

Answered By: Rick M.

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,

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.

Answered By: Kandy

open cmd in directory where you have your file and type:

pyuic5 –x "filename".ui –o "filename".py
Answered By: Seb.code

I got some errors when I try to convert UI to PY and finally I found this solution.
First of all, if you couldn’t find the pyuic5.bat file copy this code and paste it on your cmd:

C:UsersMonster>cd Desktop
C:UsersMonsterDesktop>python -m PyQt5.uic.pyuic -x trial.ui -o trial.py

And the problem has been solved easily!

Answered By: Beytullah zor

In case that you are using Pyside6, there is a ‘uic’ binary file (in windows, an exe file) in /Lib/site-packages/PySide6/

Answered By: user1098761
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.