Convert pyQt UI to python

PythonPyqtMayaPymel

Python Problem Overview


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!

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

For pyqt5 you can use

pyuic5 xyz.ui > xyz.py 

or

pyuic5 xyz.ui -o xyz.py

Solution 3 - Python

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:\Python27\Lib\site-packages\PyQt4\something> 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:\Python27\Lib\site-packages\PyQt4\something\pyuic4.exe input.ui -o output.py

Solution 4 - Python

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

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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:\Python27\Lib\site-packages\PyQt4\uic") to convert your Ui file.

> C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.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:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.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.

Solution 8 - Python

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:\Users\Monster>cd Desktop
C:\Users\Monster\Desktop>python -m PyQt5.uic.pyuic -x trial.ui -o trial.py

And the problem has been solved easily!

Solution 9 - Python

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:\python\Lib\site-packages\PyQt5\pyuic5.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!

Solution 10 - Python

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

pyuic5 –x "filename".ui –o "filename".py

Solution 11 - Python

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:\Users\ricckli.qgis2\python\plugins\qgis2leaf>C:\OSGeo4W64\bin\pyuic4 -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

Solution 12 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShannon HochkinsView Question on Stackoverflow
Solution 1 - PythonKatsuView Answer on Stackoverflow
Solution 2 - PythonAwinash jaiswalView Answer on Stackoverflow
Solution 3 - PythonhoylpraalView Answer on Stackoverflow
Solution 4 - PythonaohView Answer on Stackoverflow
Solution 5 - PythonPouyaView Answer on Stackoverflow
Solution 6 - Pythonchris mahnView Answer on Stackoverflow
Solution 7 - PythonMagSecView Answer on Stackoverflow
Solution 8 - PythonBeytullah zorView Answer on Stackoverflow
Solution 9 - PythonRick M.View Answer on Stackoverflow
Solution 10 - PythonSeb.codeView Answer on Stackoverflow
Solution 11 - PythonRiccardoView Answer on Stackoverflow
Solution 12 - PythonKandyView Answer on Stackoverflow