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

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

Related

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

GIMP 2.10 Python - How to import custom module?

I recently updated to 2.10 GIMP from 2.8, and it seems that none of my plug-ins that imported custom (relative) *.py files work anymore. I dissected the code and it's not the contents of the "path_tools.py" file, it's just the act of trying to import a custom module (even if it's an empty file it still won't work). Without this line of code it shows up in GIMP just fine:
from path_tools import *
The plugin and path_tools.py are both in the same folder
\AppData\Roaming\GIMP\2.10\plug-ins
I tried using explicit relative by adding periods
from .path_tools import *
from ..path_tools import *
from ...path_tools import *
etc.
I tried turning the file into a plug-in by adding an empty GIMP effect at the bottom of the file, to see if GIMP is just ignoring my file somehow
def path_tools(img, drw):
return
register(
"python-fu-path-tools",
"Path Tools",
"",
[...]
And by opening GIMP in command prompt
"c:\program files\gimp 2\bin\gimp-2.10" --verbose --console-messages
It just gives me a bunch of this kind of message for every plugin that won't load:
Querying plug-in: 'C:\Users\ \AppData\Roaming\GIMP\2.10\plug-ins\path_tools.py'
c:\program files\gimp 2\bin\gimp-2.10: LibGimpBase-WARNING: gimp-2.10: gimp_wire_read(): error
but those seem like warnings, especially since there is this actual error for another plug-in that wont load, for similar reasons I'm guessing:
GIMP-Error: Unable to run plug-in "export-ALL.py"
Is it related to these warnings at the top?
Parsing 'c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp'
GIMP-Warning: Bad interpreter referenced in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp: python
GIMP-Warning: Bad binary format string in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp
I just don't know what's going on, or what to do. Is there a directory that I can put the file in that it will be visible to the plugins? Because it does work to import regular modules like 'sys', 'os'.
(Sorry if this is a really stupid question, I've only used Python for GIMP plugins.)
It seems that the Python interpreter embedded with Gimp 2.10 uses / as a path separator whereas Gimp 2.10 uses \ on Windows.
The issue is discussed here.
Creating a file named C:\Program Files\GIMP 2\32\lib\python2.7\sitecustomize.py with the following content seems to fix the issue.
import sys
class Issue1542:
def __del__ (self):
if len (sys.argv[0]):
from os.path import dirname
sys.path[0:0] = [dirname (sys.argv[0])]
sys.argv = Issue1542 ()
You can hack the running file's containing directory's path into the sys.path before importing the module:
import os
import sys
sys.path.append(os.path.dirname(__file__))
import path_tools

Exception in GRPC when trying to execute Google Cloud API

I am having a problem when I generate an exe file with pyinstaller. My code runs okay when I execute using the python interpreter but when I generate the exe file using the PyInstaller I get a GRPC error.
Error:
Exception in 'grpc._cython.cygrpc.ssl_roots_override_callback' ignored
E0807 20:38:36.262000000 10808 src/core/lib/security/security_connector/security_connector.cc:1173] assertion failed: pem_root_certs != nullptr
The error happen when I try to execute a long_running_recognize.
Any tips regarding this problem?
Create hook-grpc.py in the hooks folder (\Lib\site - packages\PyInstaller\hooks) and put the following code.
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files ( 'grpc' )
If this doesn't work, then go to \Lib\site - packages\PyInstaller\hooks and add the following code to hook-google.cloud.py file.
datas += copy_metadata ( 'google-cloud-firestore' )
To resolve this I did the following
1.) create a hook-grpc with the following code
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('grpc')
2.) saved this file the location C:\Users\swap***\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyInstaller\hooks (this will vary as per the installation)
3.) Re create the exe file using the PyInstaller
command to create the file is below
pyinstaller --onefile pythonScriptName.py (replace this"pythonScriptName" with your file name)
https://github.com/grpc/grpc/issues/9223 looks similar with this issue.

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.

Convert pyQt UI to 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/

Categories

Resources