Important Note: This method is long and a lot of work for the task at hand. However, it does come with some unrelated benefits. Note that there might be a better way, but this will work.
Anyway, moving on....
You can use py2app.
Py2app
will turn your program into a .app
, meaning it runs as an application (because it is). When using tkinter
this is usually what you want in the end because GUIs are usually turned into apps for ease of use. You can read the py2app documentation here, or read a non-official but easier to understand (in my opinion) tutorial here. I will also sum up how to do the process.
First install py2app
:
Enter this into the command prompt:
sudo pip install -U py2app
If successful, you should get py2app
.
If not, one problem might be you don’t have pip
. You can download it with another command:
sudo easy_install pip
Step one:
Create a file called setup.py
in the same dictionary
as the program.
Step two:
Put this into the file.
from setuptools import setup
#APP would be the name of the file your code is in.
APP = ['example.py']
DATA_FILES = []
#The Magic is in OPTIONS.
OPTIONS = {
'argv_emulation': False,
'iconfile': 'app.icns', #change app.icns to the image file name!!!
}
setup(
app=APP,
name='Your app’s name', #change to anything
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Step 3:
Then open the bash terminal in the dictionary the file is in and type this command:
python setup.py py2app -A
The -A
makes the app respond to updates in the code, but makes the app unsharable. When you are done developing, rerun the command, this time without the -A
, like so:
python setup.py py2app
Note: You may need to use the command python3 setup.py py2...
instead of python setup.py py2...
for a python 3 py2app.
Step 4:
Navigate to the dictionary your code is in/dist
. In that folder will be your app. (The dist
folder should have been created in step three when you ran the command)
For windows users: py2app is not what should be used, instead use py2exe.
_tkinter.TclError: couldn't recognize data in image file "icon.ico"
I think the issue lies with with the file type.ico
and not the method for adding the file to tkinter. – Fania