Convert pyQt UI to python - python

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!

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

For pyqt5 you can use
pyuic5 xyz.ui > xyz.py
or
pyuic5 xyz.ui -o xyz.py

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

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

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.

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

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.

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!

Update for anyone using PyQt5 with python 3.x:
Open terminal (eg. Powershell, cmd etc.)
cd into the folder with your .ui file.
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!

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.

open cmd in directory where you have your file and type:
pyuic5 –x "filename".ui –o "filename".py

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

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

Related

How can I get a resource file (.qrc) from an image? [duplicate]

I'm having trouble importing a resource file. I'm using pyqt4 with monkey studio and I am trying to import a png image. When I run the program I get an import error like
ImportError: No module named icon_rc
I know that I have to compile it using pyrcc4 but I don't understand how to do this can anybody help please. It would be very helpful to have an answer that fully explains how to compile the resource file so I can import it.
Open cmd (or terminal on *nix) and run
pyrcc4 -py3 F:\computing\Payrollv22\icon.qrc -o icon_rc.py
It compiled the file successfully and I was able to import the py file into my project and run it with no problem.
There really isn't much to explain here, you have a resource file (e.g. icon.qrc), then you call pyrcc4 -o icon_rc.py icon.qrc which will create a module icon_rc.py which you then can import in your project.
It's all documented here.
In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py
We need to convert that qrc file into python file and then it can be imported to your code
its because when you also used pyuic5 to convert your UI to py, the resource file name from the UI sticks.
then use
Pyrcc5 input_file.qrc -o icons.py
remove from main_script.py
import icon_rc
and use
import icons
the when calling the actual icons from the icons module, you have to look at your qrc file prefix.
< RCC >
< qresource
prefix = "ico5" >
< file > plugin.png < / file >
< / qresource >
< / RCC >
if prefix is ico5
then you load icons with
QtGui.QIcon(":/ico5/plugin.png")
and if prefix is , lets say,
<RCC>
<qresource prefix="icons">
then its:
QtGui.QIcon(":/icons/plugin.png")
you could try with pyside as well like:
--- pyside-rcc -o input.qrc output.py

How to translate a file from ui to py for pyside2?

I am trying to translate a file from a ui file in python
I am writing a team
pyside-uic "C:\test.ui" -x -o "C:\test.py"
in team squad, but it tells me that there is no such team.
Changed to "pyside2-uic" but still did not help
Tell me how to fix it?
Python 3.7.6 and Pyside2.
I tried to use
import sys, pprint
from pysideuic import compileUi
pyfile = open("[path to output python file]\output.py", 'w')
compileUi("[path to input ui file]\input.ui", pyfile, False, 4,
False)
pyfile.close()
but module pysideuic not found.
What else can i do?
I haven’t installed pyside2-tools or pyside-tools for some reason, so if I need to download them somewhere, tell me how. I already tried downloading through pip but it didn’t work.
I'm not sure that I fully understood your question as "team squad" isn't clear for me.
But I'm sure that you need to install pyside2-tools as UI compiler is a part of this package.
And I want to mention that pyside-uic was removed starting from some python version (I think 3.8, but not sure).
With actual version you should use following command (linux example, but for windows it is very similar):
uic -g python -o <ouput_python_file> <input_ui_file>
<resources>
<include location="ui.files.qrc"/>
</resources>
actually, ui is folder, like this:
D:\qt5_design\project\ui\files.qrc
pyrcc5 files.qrc -o ui.files.py

Calling 2to3 in Python using cmd

I have a folder of .py files written in Python 2.7 that I want to convert to Python 3 using the 2to3 tool. Using windows 10 in the cmd prompt i can convert a single file with the following command:
C:\Users\t\Desktop\search>python.exe 2to3.py -w graphicsDisplay.py
however this line is not syntactically correct when in python shell and ideally I'd like to be able to iterate through the whole folder and update all .py files using the by using the following python code in cmd:
C:\Users\t\Desktop\search>python
>>> import os
>>> for files in os.listdir('*filepath*'):
>>> if '.py' == str(files[-3:]):
>>> *...some line of code here to perform 2to3*
its the last line which I can't seem to get right so I guess my question is, how can I call the 2to3 function in python on each iteration of the files variable?
You can do it directly from command line
for %a in (*.py) do python.exe 2to3.py -w "%a"
For each file in the indicated set execute the conversion passing the for replaceable parameter (%a in this sample) that holds the reference to the file being iterated.
Looks like 2to3 support recursive folder checking if you leave out an explicit script to convert.
Would it be easier to have all your scripts in one folder and execute against that instead?
from: https://docs.python.org/2/library/2to3.html#
2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

converting .ui files to .py file. No .py file is generated

i have been using qtdesigner with pyqt4 and python3.4. I was able to convert .ui files to .py files by using:
C:/Python34/Lib/site-packages/PyQt4/pyuic4.bat -x myfile.ui -o myfile.py
But now it has stopped working!!
The .py file is never generated. my cmd shows no error.
I also noticed that my pyuic4.bat file is 0 kb in size.
this bat-file is just executing python.
try using:
python -m PyQt4.uic.pyuic -o dest.py src.ui
python -m PyQt5.uic.pyuic -o dest.py src.ui
(see here for more options)
or consider coversion during script-start using:
from PyQt4/5.uic import compileUiDir
compileUiDir( './relative/path/to/folder/with/ui-file' )
or for single files:
from PyQt4/5.uic import compileUi
compile_ui('path/to/dir', 'file-name')

Importing resource file to PyQt code?

I have seen Qt documentary and a lot of questions less-similar to this one, But i still haven't figured out how can i do it.
I'm not entirely sure how can i import resource file to Python code, so pixmap appears without any issues.
I have all files in same directory, I created qrc. file and compiled it with: rcc -binary resources.qrc -o res.rcc to make resource file.
I imported res_rcc but pixmap on label was still not shown:
import res_rcc
This is what i had in my qrc. file:
<RCC>
<qresource prefix="newPrefix">
<file>download.jpeg</file>
</qresource>
</RCC>
Question:
How can i import resource files in the PyQt code ? | If pixmaps are in same directory as .qrc resource files, Do i still need to specify full path?
For pyqt you have to use pyrcc4, which is the equivalent of rcc for python.
pyrcc4 -o resources.py resources.qrc
This generates the resources.py module that needs to be imported in the python code in order to make the resources available.
import resources
To use the resource in your code you have to use the ":/" prefix:
Example
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import resources
pixmap = QPixmap(":/newPrefix/download.jpeg")
See The PyQt4 Resource System and The Qt Resource System
In PyQt5, we should write in comand line
pyrcc5 -o resources.py resource/resources.qrc
Because, we need to generate a resource.py to import in the code. Now we can type
import resources
in our python code
In addition to the above wonderful answers, if you would also like the ability to set the icon from within QtCreator itself (instead of having to do the say setWindowIcon(QIcon('://images/app_icon.ico')) line in code), you do this:
pyrcc5 -o resources_rc.py resources.qrc
cd ui
pyuic5 -o dialog.py dialog.ui
(Note that pyuic5 automatically imports resources_rc and not resources for some reason; hence the new name specified above)
Where you've made sure that:
...
</tabstops>
<resources>
<include location="../resources.qrc"/>
</resources>
<connections>
...
appears approximately there (between tabstops and connections) in your dialog.ui file. I think to get it there automatically, you can create a dummy C++ project and add your .ui files to the dummy project, then add a new Qt Resource file to the project. When your done, you can delete everything leaving the .ui files and the .qrc file. If you happen to copy resources.qrc to another directory, then closing and re-opening the dialog.ui file will prompt you for where the new location is.
Now you can set the resources from the Property explorer on in QtCreator: windowIcon > Choose Resource > (click on the root) > (your files should show up now) > (select app_icon.ico).
I just checked a newly created mainwindow.ui, if you open up the file in Text Edit mode in Qt Creator it shows you where the <resource /> stub is. Simply insert there (using some other program) For some reason opening up the newly created .ui file in Notepad++ was not showing it.
When closing and re-opening files, you must actually close the file (not "Reload" - doesn't work) and open it again. Then the resource root in "add image from resources" dialog will be non-empty.

Categories

Resources