Set Tkinter Python application icon in Mac OS X - python

I have created a Tkinter GUI in Python. The following snippet shows how when the application starts it changes its icon to the one in icon.gif. This works on Ubuntu and Windows however it does not appear to do anything in Mac. If it helps I'm running Python 2.7.10 on OS X 10.9.5 with Tcl 8.5 & Tk 8.5 (8.5.18). How can I change the icon that appears in the dock?
import Tkinter as TK
class MyClass(object):
def __init__(self, root):
pass
if __name__ == '__main__':
root = TK.Tk()
my_app = MyApp(root)
icon_path = 'some\long\path\to\icon.gif'
img = TK.PhotoImage(file=icon_path)
root.tk.call('wm', 'iconphoto', root._w, img)
root.mainloop()
root.destroy()

I know this is old, but i just saw the answer before coming here and I thought I'd share the answer in case someone stumbles on this in the future and wants to know.
You must supply a icon file in the .icns format in order for your app to have an application icon. py2app has three ways of doing this:
command line argument
options dictionary in setup.py
setup.cfg file
In all following situations, replace app.icns with the full path to your desired .icns file
CLI argument
$ python setup.py py2app --iconfile app.icns
Setup.py dictionary
from setuptools import setup
APP = ['YourApp.py']
APP_NAME = "YourApp"
DATA_FILES = []
OPTIONS = {
'argv_emulation': True,
'iconfile': 'app.icns',
}
setup(
name=APP_NAME,
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Setup.cfg
I'm honestly not sure about this one, I don't touch .cfg files
[py2app]
iconfile="app.icns"
Here is the relevant relevant documentation. A practical example is available as well.

If you mean the icon in the top left corner this might not work but to create an app for Mac with an icon you do this:
To add an icon you have to make a .icns file. Follow these or these instructions. Secondly you have to make the program into an app. To do this follow the instructions here but don't do it yet. Ok now go into setup.py and add under options 'iconfile' : 'icon.icns'. Create the app now. It will only work if you put the .icns image in the same folder as setup.py. This will add an icon to the app.

Related

I'm using py2app to make my pygame executable but it keeps giving me an error that the file or directory isn't found [duplicate]

This question already has an answer here:
py2app-installed app using pygame fails
(1 answer)
Closed 1 year ago.
Title, I just finished my first pygame project which Is a very simple game with a main menu module and a main game module, they both run perfectly when I run them from sublime.
I tried using py2app to share this game with some friends, after installing py2app this is what I did in my terminal:
py2applet --make-setup MainMenu.py # (this module imports the main game module)
then I edited my setup.py file to include all my python files like images, sounds and fonts (this is the setup.py file):
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['MainMenu.py']
DATA_FILES = ['MySprite/Shoot/1.png','MySprite/Shoot/2.png',
'MySprite/Go/1.png','MySprite/Go/2.png','MySprite/Go/3.png','MySprite/Go/4.png','MySprite/Go/5.png','MySprite/Go/6.png','MySprite/Go/7.png','MySprite/Go/8.png',
'go_1.png','go_2.png','go_3.png','go_4.png','go_5.png','go_6.png','go_7.png','go_8.png','go_9.png','go_10.png','go_1L.png','go_2L.png','go_3L.png','go_4L.png','go_5L.png','go_6L.png','go_7L.png','go_8L.png','go_9L.png','go_10L.png','HomeScreen.png','icon.png','Bullet.png','SkyNight.png','GamePlatform.png','Heart.png','Starjedi.ttf','Chernobyl.ttf',
'MainMenu.wav','Fireball.wav','background.wav','Jump.wav','Zombie.wav','Hit.wav']
OPTIONS = {}
setup(
app = APP,
data_files = DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Then back to my terminal:
rm -rf build dist
then:
python setup.py py2app -A
which gave me an error so I changed it to python3 and it created a "build" and a "dist" folders, I tried opening the app inside the "dist" folder but it gave me an error and asked me to terminate or open the console, I right clicked on it and pressed show package options --> contents --> MacOs and pressed the file which opened another terminal with the error:
screen_home = pygame.image.load('HomeScreen.png')
FileNotFoundError: No such file or directory.
Both modules run perfectly from sublime, Im out of ideas and I tried googling the problem and I can't find a solution, Im new at this so I apologize if the solution was obvious or it was a dumb question and id appreciate any help.
I don't have a real answer but a few things to try and check:
We need to review a few things in the two lines, sometimes the setup.py requires to have the full path where each file is located, it is the right way, otherwise the setup.py will not find where are those images and it won't be able to include them into the final package, Are all those files in the same folder?, even if they are, try to add the full path for each file (jbsidis should be replaced by your username):
import os
APP = ['/home/jbsidis/Escritorio/MainMenu.py']
if os.path.isfile(APP[0])==True:
print("the MainMenu.py file it does exists")
DATA_FILES = ['/home/jbsidis/Escritorio/MySprite/Shoot/1.png','/home/jbsidis/Escritorio/MySprite/Shoot/2.png','/home/jbsidis/Escritorio/MySprite/Go/1.png','/home/jbsidis/Escritorio/MySprite/Go/2.png','/home/jbsidis/Escritorio/MySprite/Go/3.png','/home/jbsidis/Escritorio/MySprite/Go/4.png','/home/jbsidis/Escritorio/MySprite/Go/5.png','/home/jbsidis/Escritorio/MySprite/Go/6.png','/home/jbsidis/Escritorio/MySprite/Go/7.png','/home/jbsidis/Escritorio/MySprite/Go/8.png','/home/jbsidis/Escritorio/go_1.png','/home/jbsidis/Escritorio/go_2.png','/home/jbsidis/Escritorio/go_3.png','/home/jbsidis/Escritorio/go_4.png','/home/jbsidis/Escritorio/go_5.png','/home/jbsidis/Escritorio/go_6.png','/home/jbsidis/Escritorio/go_7.png','/home/jbsidis/Escritorio/go_8.png','/home/jbsidis/Escritorio/go_9.png','/home/jbsidis/Escritorio/go_10.png','/home/jbsidis/Escritorio/go_1L.png','/home/jbsidis/Escritorio/go_2L.png','/home/jbsidis/Escritorio/go_3L.png','/home/jbsidis/Escritorio/go_4L.png','/home/jbsidis/Escritorio/go_5L.png','/home/jbsidis/Escritorio/go_6L.png','/home/jbsidis/Escritorio/go_7L.png','/home/jbsidis/Escritorio/go_8L.png','/home/jbsidis/Escritorio/go_9L.png','/home/jbsidis/Escritorio/go_10L.png','/home/jbsidis/Escritorio/HomeScreen.png','/home/jbsidis/Escritorio/icon.png','/home/jbsidis/Escritorio/Bullet.png','/home/jbsidis/Escritorio/SkyNight.png','/home/jbsidis/Escritorio/GamePlatform.png','/home/jbsidis/Escritorio/Heart.png','/home/jbsidis/Escritorio/Starjedi.ttf','/home/jbsidis/Escritorio/Chernobyl.ttf','/home/jbsidis/Escritorio/MainMenu.wav','/home/jbsidis/Escritorio/Fireball.wav','/home/jbsidis/Escritorio/background.wav','/home/jbsidis/Escritorio/Jump.wav','/home/jbsidis/Escritorio/Zombie.wav','/home/jbsidis/Escritorio/Hit.wav']
for x in DATA_FILES:
if os.path.isfile(x)==True:
print("the "+str(x)+" file it does exists")
else:
print("the "+str(x)+" DOES NOT Exists in that location and that's why you got the error with your setup.py")
Here is the image in my case, my folders don't have those files, so the result is:

Application fails to start after building using cx_Freeze on OSX

I'm trying to build a simple app on OSX using cx_Freeze. The build using setup.py seems to go fine - it builds a .dmg and .app for my application, along with all of the source files. However, when I try to run the .app, it crashes immediately, saying "My_App quit unexpectedly". Frustratingly, I don't see any decipherable error codes I can track down.
I have stripped down the app into a very simple example. Below are my main python file and my setup file.
my_app.py
from tkinter import *
root = Tk()
root.title("Welcome to My_App")
root.geometry('350x200')
root.mainloop()
setup.py
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": [], "excludes": []}
# base="Win32GUI" should be used only for Windows GUI app
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "My_GUI",
version = "1.0",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("my_app.py", base=base)]
)
I have built an .exe on windows before, and when startup fails on windows, I at least get a traceback error message that I can track down. On OSX, it doesn't appear to do the same thing.
What's going wrong? How can I get more information on why my app is failing to start up? Below are images of my build and the error I'm getting on startup.
Ok turns out if I run the file from terminal, it outputs an actual error code. From there, I was able to track down that I did not have zlib installed. I was getting error
zipimport.ZipImportError: can't decompress data; zlib not available
What ultimately solved it for me was reading through this thread
https://github.com/jiansoung/issues-list/issues/13

pyGTK app looks different frozen using cx_freeze

My application looks different after I freeze it using cx_freeze (I'm using Gui2Exe to create and run the cx_freeze script) and when I run it using the python interpreter.
[As I'm not allowed to post images, here are the links to the UI, edit?]
Script run:
Application as run from the command line
Frozen run:
Application as run after being frozen by cx_freeze
I have tried both including and not including the manifest file in the cx_freeze script, yet I'm unsure what could be causing the application UI to change so dramatically.
Here is the cx_freeze script:
# ======================================================== #
# File automagically generated by GUI2Exe version 0.5.3
# Copyright: (c) 2007-2012 Andrea Gavana
# ======================================================== #
# Let's start with some default (for me) imports...
from cx_Freeze import setup, Executable
# Process the includes, excludes and packages first
includes = ['ast', 'gobject', 'gtk']
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = ['BeautifulSoup', 'mechanize', 'pygtk']
path = []
# This is a place where the user custom code may go. You can do almost
# whatever you want, even modify the data_files, includes and friends
# here as long as they have the same variable name that the setup call
# below is expecting.
# No custom code added
# The setup for cx_Freeze is different from py2exe. Here I am going to
# use the Python class Executable from cx_Freeze
GUI2Exe_Target_1 = Executable(
# what to build
script = "moodle-downloader.py",
initScript = None,
base = 'Win32GUI',
targetDir = r"md",
targetName = "moodle-downloader.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = True,
appendScriptToLibrary = True,
icon = r"C:\Users\Nasser.Al-Hilal\Dropbox\CodeN\Projects\Applications\Personal\MoodleDownloader\res\md.ico"
)
# That's serious now: we have all (or almost all) the options cx_Freeze
# supports. I put them all even if some of them are usually defaulted
# and not used. Some of them I didn't even know about.
setup(
version = "0.3",
description = "An app to assist in downloading assignment submissions from Moodle LMS.",
author = "Nasser Al-Hilal",
name = "Moodle Downloader",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": path
}
},
executables = [GUI2Exe_Target_1]
)
# This is a place where any post-compile code may go.
# You can add as much code as you want, which can be used, for example,
# to clean up your folders or to do some particular post-compilation
# actions.
# No post-compilation code added
# And we are done. That's a setup script :-D
I would prefer if I can get the application to look the same as when run from the interpreter.
For you to work you have to download gtkruntime, install it on your system, then copy the lib folder and share your generated build folder, then copies the contents of the bin folder gtkruntime your build folder, and then you work.
My installation:
windows 7
Python 2.7.9
GTK2 (python -m pip install pygtk)
pycairo (1.8.10) pygobject (2.28.3)
pygoocanvas (0.14.2)
pygtk (2.24.0)
pygtksourceview (2.10.1)
cx-Freeze (python -m pip install cx-Freeze)
cx-Freeze (4.3.4)
cx-freeze compile command:
\Py279\Scripts\cxfreeze MyGUIapp.py --target-dir somewheredir --base-name=Win32GUI
(in essence, all done without any setup command files, although some can be created :) )
once cx-freeze compiles to exe and copy dll it believes that are needed I get motif/x11 look/feel however, when I add to somewheredir all .dll files from:
\Py279\Lib\site-packages\gtk-2.0\runtime\lib\gtk-2.0\2.10.0\engines\
to:
somewheredir\lib\gtk-2.0\2.10.0\engines\
I get windows look and feel.
Files found there in my case are:
libpixmap.dll
libsvg.dll
libwimp.dll
that's all.
Plus:
some further design customization influence can be achieved by creating .gtkrc-2.0 in the user's home folder c:\Users\myuser. Namely one can influence default font face and size used, window background color and such. For example, following code (hints from elsewhere)
style "win32-font" {
# make default font larger
font_name = "Sans 12"
# set the background to a light grey
bg[NORMAL] = "#f6f6f6"
}
class "*" style "win32-font"
placed in c:\Users\myuser\.gtkrc-2.0 would change 'default' font size and change top window background for pygtk applications run by user (meaning each user can set its own preferences there).
This is just a translation of Blasito's answer:
"For this to work correctly, first download and install GTK-runtime on your computer. Then navigate to GTK-runtime installation folder (in Program Files) and copy over the lib and share folders over to the generated build folder. Finally, copy the contents of the bin folder (in the installation directory) into the build folder."
I can verify this works.

PyQt/PySide - icon display

I have a PySide app which has an icon for the MainWindow (a QMainWindow instance). When I run the file normally, the icon is visible and everything is fine but when I create an exe with py2exe, the icon does not appear. This happens with cx_freeze also( so I don't think the problem's with py2exe).
The app was designed using QtDesigner and converted to python with pyside-uic. I tried both using icons as a file and as a resource(qrc file) and both don't seem to work.
Any help or pointers would be appreciated.
Thanks.
kochelmonster's solution works so long as you don't try and bundle the Qt dlls into library.zip or the exe. You also don't need to set a library path if you put the plugins in the base of the app directory.
I still wanted to bundle everything else so I excluded the qt dlls and added them manually. My setup.py looks something like this:
from os.path import join
_PYSIDEDIR = r'C:\Python27\Lib\site-packages\PySide'
data_files =[('imageformats',[join(_PYSIDEDIR,'plugins\imageformats\qico4.dll')]),
('.',[join(_PYSIDEDIR,'shiboken-python2.7.dll'),
join(_PYSIDEDIR,'QtCore4.dll'),
join(_PYSIDEDIR,'QtGui4.dll')])
]
setup(
data_files=data_files,
options={
"py2exe":{
"dll_excludes":['shiboken-python2.7.dll','QtCore4.dll','QtGui4.dll'],
"bundle_files": 2
...
}
}
...
)
If your project uses additional Qt dlls you will have to exclude and manually add them as well. If you need to load something other than an .ico image you'll also need to add the correct plugin.
I'm assuming it works with a bmp, but not a png/jpg? If so, it's likely that the image format plugins don't load properly.
I'd guess setting up a qt.conf file in the installed application's directory and making sure plugin-dll's go to /plugins/imageformats/ will make things work better.
I had the same problem. After some investigation I found a solution:
(Macke had the right idea)
cx_freeze does not copy the PyQt plugins directory, which contains the ico image reader.
Here are the steps:
in setup.py copy the PyQt4 plugins directory to your distribution
In your code write something like:
application_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
try:
if sys.frozen:
plugin_path = os.path.join(application_path, "qtplugins")
app.addLibraryPath(plugin_path)
except AttributeError:
pass
Could it be related to Windows 7's taskbar icon handling?
See How to set application's taskbar icon in Windows 7 for an answer to that.
You must include "qico4.dll" manually in your release folder. Insert this in your setup.py:
import sys
from os.path import join, dirname
from cx_Freeze import setup, Executable
_ICO_DLL = join(dirname(sys.executable),
'Lib', 'site-packages',
'PySide', 'plugins',
'imageformats', 'qico4.dll')
build_exe = {
'include_files': [(
_ICO_DLL,
join('imageformats', 'qico4.dll'))]}
setup(name = "xxxxx",
version = "1.0.0",
...
options = { ...
'build_exe': build_exe
...},
...)

How can I hide the console window when freezing wxPython applications with cxFreeze?

I'm developing a Python application using wxPython and freezing it using cxFreeze. All seems to be going fine apart from this following bit:
When I run the executable created by cxFreeze, a blank console window pops up. I don't want to show it. Is there any way I could hide it?
It doesn't seem to be documented on the cxFreeze site and Googling didn't turn up much apart from some similar sorta problems with Py2Exe.
Thanks.
For Windows:
You have to use a line like this (use file folders and names as appropriate)
C:/Python/Scripts/cxfreeze C:/Python/Code/yourprogram.py --base-name=Win32GUI --target-dir C:/Python/Dist
By adding the --base-name=Win32GUI option, the console window will not appear.
This worked to some extent but it has issues. My program runs in both a console mode and a GUI mode. When run from the console with a --console argument it runs in a console mode. When I followed the procedure below, this doesn't work anymore and my program is only a GUI app then.
The following source code comes from a sample file in the \Python\Lib\site-packages\cx_Freeze\samples\PyQt4\setup.py. Lesson of the day. Read the README.
# A simple setup script to create an executable using PyQt4. This also
# demonstrates the method for creating a Windows executable that does not have
# an associated console.
#
# PyQt4app.py is a very simple type of PyQt4 application
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the application
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "simple_PyQt4",
version = "0.1",
description = "Sample cx_Freeze PyQt4 script",
executables = [Executable("PyQt4app.py", base = base)])
If you're using Windows, you could rename your "main" script's extension (that launches the app) to .pyw
Option 1) Use gui2exe to muck with various options.
Option 2) Modify your setup.py with 'base' parameter as such.
GUI2Exe_Target_1 = Executable(
# what to build
script = "rf_spi.py",
initScript = None,
base = 'Win32GUI', # <-- add this
targetDir = r"dist",
targetName = "rf_spi.exe",
compress = True,
copyDependentFiles = False,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = r"wireless.ico"
)

Categories

Resources