8

I am developing a game in Python and was wondering how to give it its own icon. I am using a windows computer and have no extra things installed with Python. Oh also I am using version 3.3 Is this even possible.

P.S I have found other things on Stack Overflow but they are using different Operating Systems like Ubuntu and Macintosh

fg_
  • 59
  • 1
  • 8
Ethan Bacon
  • 233
  • 1
  • 2
  • 7
  • 2
    What do you mean by an 'icon'? An icon in the terminal window? An icon on the script? An icon on the executable file? (if so, what made the executable?) An icon on the window? (If so, what GUI toolkit?) Some other icon? – Gareth Latty May 28 '13 at 00:10
  • On the desktop instead of having the python script icon have my own .ico image instead – Ethan Bacon May 28 '13 at 00:11
  • But what code do i use to do this? – Ethan Bacon May 28 '13 at 00:11

7 Answers7

5

You can't add a custom icon to a plain Python script, but if you convert it to a Windows executable using py2exe, you can specify icon resources to use for it. There's a how-to on their wiki.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
5

There are two steps: first build the Python executable. For this you will need something like py2exe, "which converts Python scripts into executable Windows programs, able to run without requiring a Python installation."

Then once you have your executable, to give it an icon, you can use the answer to this question: Add icon to existing EXE file from the command line for that finishing touch.

Community
  • 1
  • 1
Dmitri
  • 2,658
  • 2
  • 25
  • 41
5

Update (2021-11-3):

Py2Exe new versions are now available so @dmitris's solution will now work although PyInstaller is another option. However the Python 3.5 series of Py2Exe ended on September 30, 2020.


Original Answer:

@dmitri's solution works but Py2Exe stopped development at python 3.4 and will not work with newer versions PyInstaller would also do this.

pip install pyinstaller
pyinstaller --onefile --windowed --icon=youricon.ico yourprogram.py

Python version 3.7.3.

recurseuntilfor
  • 2,293
  • 1
  • 12
  • 17
2

If you are trying to change the icon of the shortcut for your program,

  • then you need to get to the file where ever it is right-click it and go to create a shortcut

  • then drag that shortcut to your desktop

  • then right-click that shortcut and click properties

  • then click on "Change Icon"

  • then go to where your desire .ico image is saved and set that as the icon

if you do this and you open your program in the corner will be the .ico you selected and on your desktop, it will show the icon instead of the python image.

that is how you change the shortcut icon but there is no way to change the actual window icon in the corner unless you're using something like Tk or Pygame.

Mihir Ajmera
  • 127
  • 1
  • 9
Serial
  • 7,925
  • 13
  • 52
  • 71
1

You could write a small VBS script to create the shortcut, and assign the icon you want. For example, see the use of strIconPath at http://www.computerperformance.co.uk/ezine/ezine119.htm.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • Thank you!! That's the best solution for me since it is quite simple - I just call the vbs script in my Python code! No need for a big py2exe and its huge output executable! – user26742873 Jul 30 '21 at 08:13
0

You can't. In Windows, custom icons can only be assigned to certain types of files (executables and shortcuts, mostly), and Python scripts are not one of those types.

0

You cannot add icons to scipts, but for your purpose, you could also consider converting your script into an exe. There are many ways to do this

One of the easiest solutions to this is auto-py-to-exe, its a user-friendly, easy gui solution to converting ".py" to ".exe", it also allows you to choose icons, additional files etc. very easily.

To install using pip just enter this in the cmd prompt

pip install auto-py-to-exe

To run the program after installation, enter this in cmd prompt

auto-py-to-exe

This article explains it step by step in an easy way.

Dragon-KK
  • 16
  • 2