如何在 Python 程序中添加我自己的图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16782047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:40:19  来源:igfitidea点击:

How To Add An Icon Of My Own To A Python Program

python

提问by Ethan Bacon

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.

我正在用 Python 开发一个游戏,想知道如何给它自己的图标。我正在使用 Windows 计算机并且没有安装 Python 的额外东西。哦,我也在使用 3.3 版,这甚至可能吗?

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

PS 我在 Stack Overflow 上发现了其他东西,但他们使用的是不同的计算机,如 Ubuntu 和 Macintosh

采纳答案by Dmitri

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."

有两个步骤:首先构建 Python 可执行文件。为此,您将需要类似py2exe 的东西,“它将 Python 脚本转换为可执行的 Windows 程序,无需安装 Python 即可运行。”

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 linefor that finishing touch.

然后一旦你有了你的可执行文件,给它一个图标,你可以使用这个问题的答案:从命令行将图标添加到现有的 EXE 文件中以进行最后的润色。

回答by duskwuff -inactive-

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.

你不能。在 Windows 中,自定义图标只能分配给某些类型的文件(主要是可执行文件和快捷方式),而 Python 脚本不是这些类型之一。

回答by Serial

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

如果您正在尝试更改程序快捷方式的图标

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

  • then drag that shortcut to youre 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

  • 然后你需要找到它所在的文件,右键单击它并去创建快捷方式

  • 然后将该快捷方式拖到您的桌面

  • 然后右键单击该快捷方式并单击属性

  • 然后点击“更改图标”

  • 然后转到保存您想要的 .ico 图像的位置并将其设置为图标

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

如果你这样做并且你在角落里打开你的程序将是你选择的 .ico 并且在你的桌面上它会显示 ico 而不是 python 图像

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

这就是您更改快捷方式图标的方式,但除非您使用 Tk 或 Pygame 之类的东西,否则无法更改角落中的实际窗口图标

回答by Cairnarvon

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.

您无法将自定义图标添加到纯 Python 脚本,但如果您使用py2exe将其转换为 Windows 可执行文件,则可以指定要使用的图标资源。他们的 wiki上有一个操作方法。

回答by jarmod

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.

您可以编写一个小的 VBS 脚本来创建快捷方式,并指定您想要的图标。例如,请参阅http://www.computerperformance.co.uk/ezine/ezine119.htm中 strIconPath 的使用。

回答by recurseuntilfor

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

@dmitri 的解决方案有效,但 Py2Exe 在 python 3.4 上停止开发,并且不适用于较新版本 PyInstaller 也会这样做。

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

Python version 3.7.3.

Python 版本 3.7.3。