I have a small program in PyQt4 and I want to compile the program into an Exe. I am using py2exe to do that. I can successfully set icon in the windows title bar using the following code, but when i compile it into exe the icon is lost and i see the default windows application. here is my program:
import sys
from PyQt4 import QtGui
class Icon(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('c:/python26_/repy26/icons/iqor1.ico'))
app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())
**** Here is the setup.py for py2exe****
from distutils.core import setup
import py2exe
setup(windows=[{"script":"iconqt.py"
,"icon_resources": [(1, "Iqor1.ico")]}]
,options={"py2exe":{"includes":["sip", "PyQt4.QtCore"]}})
The problem is that py2exe doesn't include the qt icon reader plugin. You need to tell it to include it with the data_files parameter. Something along these lines:
setup(windows=[{"script":script_path,
"icon_resources":[(1, icon_path)]}],
data_files = [
('imageformats', [
r'C:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qico4.dll'
])],
options={"py2exe":{"packages":["gzip"],
"includes":["sip"]}})
I believe you need to reference the .ico file directly from the EXE or DLL that you are creating with py2exe. You seem to have the setup.py script correct, so take a look at: http://www.py2exe.org/index.cgi/CustomIcons. There is an example for wxWidgets, but you could try to adapt it to Qt.
I would suggest you to create a file called YourApp.rc, add up the following line :
IDI_ICON1 ICON DISCARDABLE "res/icons/app_icon.ico"
Then in your .PRO file, add up the following lines :
win32{
RC_FILE = YourApp.rc
}
It should fix your problem !
I had the same issue. For some reason it worked just fine with a image.png file & not an image.ico file. No clue why. But i converted the ico to png & it worked
Related
I am creating a GUI using Qt Designer (ver. 5.9.7) in macbook (OS Monterey 12.2.1).
When I convert the .ui file into .py file via pyuic5, the output .py file always contains 'import Penta_gif_02.gif' in the very last line, but have no idea where this weird import line came from.
The gif file is my own gif file which has nothing to do with this .ui file (have no idea how this local image ended up in my .ui file!)
Presumably, I wrongly added my local gif file into .ui file at some point but cannot find where I can remove it.
Could anyone help me out please. Thanks.
Here is the summary of my output Qt .py code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'SView.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#... *snip* ...#
def retranslateUi(self, MainWindow):
#... *snip* ...#
self.tab_pca.setTabText(self.tab_pca.indexOf(self.tn_graph), _translate("MainWindow", "Graph"))
self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
self.actionPreference.setText(_translate("MainWindow", "Preference"))
import Penta_gif_02_rc
I fixed the problem thanks to the ekhumoro's comment!
Here is a solution Just for people who are as noob as I am.
Open the .UI file via Qt Designer.
From the Resource Browser window, go to 'edit resources'.
Select the resource which you want to delete, and then click the 'remove' button.
I wrote a lot of custom widget plug-ins for using them in the Qt Designer and my Pipeline.
This is working fine on my Mac (Mavericks, PyQt4, Python 2.7), and this week I wanted to implement those plugins on my Windows environment as well. But it wasn't working. The plug-ins are not appear in the Qt Designer's Widget Box on the left (Windows 7, PyQt4, Python 2.7).
After a lot of try's, I downloaded the PyQt4 example files and followed the instructions from: PyQt Reference Guide and also the instructions
from the example launcher file examples/designer/plugins/plugins.py itself, but it was still not working.
So I copied the the following example files:
plugin files to "C:\designer_plugins"
widget files to "C:\designer_widgets"
Just to making the code as simple as possible to figure out what is going wrong.
So, this is my testing plugins.py file:
#!/usr/bin/env python
import sys
import os
from PyQt4 import QtCore, QtGui, uic
env = os.environ.copy()
env['PYTHONPATH'] = r"C:\designer_widgets" #("%s"%os.pathsep).join(sys.path)
env['PYQTDESIGNERPATH'] = r"C:\designer_plugins"
qenv = ['%s="%s"' % (name, value) for name, value in env.items()]
# Start Designer.
designer = QtCore.QProcess()
designer.setEnvironment(qenv)
designer_bin = r"C:\Python27x64\Lib\site-packages\PyQt4\designer.exe"
designer.start(designer_bin)
designer.waitForFinished(-1)
sys.exit(designer.exitCode())
I thought that my designer object didn't get the right path, so I implemented the folowing to check it's environment:
# Check if paths are right
print "\n # Designer Env:"
for pypath in designer.environment():
if "PYTHONPATH" in pypath:
print " # ",pypath
if "PYQTDESIGNERPATH" in pypath:
print " # ",pypath
Console output:
# Designer Env:
# PYQTDESIGNERPATH="C:\designer_plug"
# PYTHONPATH="C:\designer_widgets"
The paths are right. And like I said, on my mac it works perfectly.
I was trying this also with different Qt Designer installations. But both (PyQt4 and PySide) designers doesn't show any of the example plugins. Both Qt Designer couldn't find the example plugins. I double checked it inside the designer with Help/About Plugins).
Any ideas what I did wrong? Or is this generally not working on a Windows 7 System?
All fixed. I rebuild PyQt4, so I think I made probably a mistake in the source code of Qt in the past.
I am fairly new to python and have made a simple UI using pyside. When run from inside the anaconda IDE the UI works fine, but when I run it using anaconda from the command line \python.exe 'runquacker.py' the UI flashes up and disappears immediately.
The initial script is:
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import quacker
class MainDialog(QDialog, quacker.Ui_Dialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
app = QApplication(sys.argv)
form = MainDialog()
form.show()
The rest of the UI is in quacker.py which collects a bunch of variables from the user, before executing a further analysis.py program using a subprocesscall. The variables are all passed in this way because thats the only way I could get pyside to work with my script!
e.g. For two variables 'plots' and 'block':
subprocess.call([sys.executable, 'd:\\py\\anaconda\\analysis.py', str(plots), str(block)], shell=True)
Ive tried putting a raw_input('Blah..') in a few places but it just causes the program to hang or nothing at all.
Using \python.exe -i runquacker.py also causes the program to hang.
Thanks
You need to add this line at the end of your script: app.exec_()
That's because you need to actually execute your Qt application if you whant to see something.
I'm not pretty sure why it works in Anaconda but if you are using some IDE like Spyder I think it works because Spyder is already running in Qt (so it called QApplication.exec_ before).
I'm working on an applcation in Python's PyQt4 and cannot find how to change the taskbar icon. I made my .ui files in Qt's Designer, where I can change the windowIcon properties. But that is not what I am looking for. I want to change the look of the application's icon in windows taskbar. For now it is Python logo in a window icon.
I found some information on SO: link but it's not helping me much.
I tried:
app = QtGui.QApplication([])
app.setWindowIcon(QtGui.QIcon('chip_icon_normal.png'))
app.exec_()
But the icon remains unchanged.
What i want to change, showing the picture:
(This is done calling the setWindowIcon on main window/ dialog, or the application, as shown above.)
This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.
See this answer for details, along with a workaround using ctypes.
It seems to me that the problem may be caused by lack of icon with the right size.
The following setup worked for me in PyQT4:
# set app icon
app_icon = QtGui.QIcon()
app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
app.setWindowIcon(app_icon)
I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.
You need to call setWindowIcon(...) on the window, not on the application.
Here's an example, which works for me:
#!/usr/bin/env python3
import os
import sys
import subprocess
import os.path
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWin(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.setWindowTitle("My Window")
self.setWindowIcon(QtGui.QIcon('test_icon.png'))
self.show()
def main(args):
app = QtGui.QApplication([])
ww= MyWin()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv[1:])
For me, the following code works for both changing task bar icon and window icon
win.setWindowIcon(QIcon('logo.png'))
I know this is very simple, just use the command self.set_icon_from_file("icon.png"), however my program still does not display the icon. I made sure the icon.png is in the same working directory as the Python file. I also tried giving the complete file path, but still it does not display the icon.
I am using Ubuntu 10.10 if that helps and using Python V2.6. I use Glade Interface Designer to design the GUI. However, I tried setting the icon both using Glade and using the command above.
I hope I have provided sufficient information.
EDIT: I got the status icon to work in my program.. However in the question I meant the program icon displayed in the task bar and also on the left side of the application bar.
I made sure the icon.png is in the same working directory of the python file.
This may be your problem — paths are looked up relative to the working directory of the Python interpreter, not the file containing the code. I often find myself defining a function like:
def get_resource_path(rel_path):
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
Mine isn't actually quite that verbose, but hopefully the variable names make it clear what's going on. Also, getting the absolute path isn't strictly necessary, but might help if you need to debug.
Then you can just do:
self.set_icon_from_file(get_resource_path("icon.png"))
Update: Here is a demo program. "icon.png" is in the same directory as this script, and I run it using ./gtktest.py. I see the icon in the top left corner (standard place for my theme). icon.png is just a shape drawn in Inkscape and exported as a bitmap (it works with the original SVG too, anyway).
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class HelloWorld:
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_icon_from_file('icon.png')
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
# Creates a new button with the label "Hello World".
self.button = gtk.Button("Hello World")
self.window.add(self.button)
self.button.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
hello = HelloWorld()
hello.main()
I am not sure what icon you are creating, but try this smallest PyGTK icon-showing example of taskbar icon I have thought of:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
# create icon object
statusIcon = gtk.StatusIcon()
# load it
statusIcon.set_from_file("icon.ico")
# show it
statusIcon.set_visible(True)
# and run main gtk loop
gtk.main()
Maybe you just missed the command statusIcon.set_visible(True)
For standard icons, use stock items, and find icons that suits your needs. that way
You don't have to pack icons whith your program
The icons change
according to the user's theme and will blend nicely in her
environment.
for pyGTK :
gtk.icon_theme_get_default().load_icon("folder-open", 128, 0)