简体   繁体   English

更改应用程序和任务栏图标 - Python/Tkinter

[英]Changing the application and taskbar icon - Python/Tkinter

I've been working on a very simple python/tkinter script (a .pyw file) and I'd like to change it's application icon (the 'file' icon shown at the explorer window and the start/all programs window, for example - not the 'file type' icon nor the main window of the app icon) and the taskbar icon (the icon shown at the taskbar when the application is minimized).我一直在研究一个非常简单的 python/tkinter 脚本(一个 .pyw 文件),我想更改它的应用程序图标(例如,资源管理器窗口和启动/所有程序窗口中显示的“文件”图标- 不是“文件类型”图标,也不是应用程序图标的主窗口)和任务栏图标(应用程序最小化时任务栏上显示的图标)。 Is it possible to change them or is it something only doable when you effectively install an application through an .exe?是否可以更改它们,或者只有当您通过 .exe 有效安装应用程序时才可行?

This little app is supposed to run on Windows XP / 7 machines only and it's in Python 2.7.3.这个小应用程序应该只能在 Windows XP / 7 机器上运行,它使用 Python 2.7.3。

Thanks in advance!提前致谢!

Another option on Windows would be the following: Windows 上的另一个选项如下:

To your python code add the following:在您的 python 代码中添加以下内容:

import ctypes

myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

Use利用

root.iconbitmap(default='ardulan.ico')

But the probleme is that it only replace the icon on the windows not on the taskbar.但问题是它只替换了窗口上的图标而不是任务栏上的图标。 It's because the py file is executed from python interpreter so windows use his icon and not the tkinter icon.这是因为 py 文件是从 python 解释器执行的,所以 windows 使用他的图标而不是 tkinter 图标。

You have to 'compile' it i think with py2exe, cx_Freeze, py2app ...我认为你必须用 py2exe、cx_Freeze、py2app 来“编译”它...

http://www.py2exe.org/index.cgi/CustomIcons http://www.py2exe.org/index.cgi/CustomIcons

I have a music_app.py file and melody.ico file我有一个music_app.py文件和melody.ico文件

With Tkinter you can use:使用 Tkinter,您可以使用:

from tkinter import *
root = Tk()
root.title("melody")
root.iconbitmap(r"melody.ico")
root.mainloop()

With PyQT使用 PyQT

from PyQt4 import QtGui
import sys

app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
Form.setWindowIcon(QtGui.QIcon('melody.ico'))
Form.setWindowTitle('melody')
Form.show()
sys.exit(app.exec_())

After that you can convert music_app.py to .exe or not and the result remain the same:之后,您可以将music_app.py转换为.exe或不转换,结果保持不变:

在此处输入图像描述

Hope it help !!!希望有帮助!!!

You can do that by creating a new shortcut to the python.exe or pythonw.exe file, (option available in Window's explorer right-click menu), then right click the shortcut, select properties, and change target to:您可以通过创建python.exepythonw.exe文件的新快捷方式来做到这一点,(在 Window 的资源管理器右键单击菜单中可用的选项),然后右键单击快捷方式,选择属性,并将目标更改为:

"C:\Path\to\Python.exe" "Absolute\or\relative\path\to\file.py"

Then select change icon (still in the properties window), and select your .ico file.然后选择更改图标(仍在属性窗口中),然后选择您的 .ico 文件。 Executing the program from this shortcut will automaticaly set the selected icon as taskbar and window icon.从此快捷方式执行程序将自动将所选图标设置为任务栏和窗口图标。

Note though that by executing set shortcut, instead of clicking directly your .py file, the current working directory will be the one of python.exe , and not of your file, you can change that too in the shortcut's properties window, in the "start in" entry field (underneath target), or by using the python methods os.getcwd() to find the current working directory and os.chdir(path) to set it请注意,虽然通过执行设置快捷方式,而不是直接单击您的 .py 文件,当前工作目录将是 python.exe 之一,而不是您的文件,您也可以在快捷方式的属性窗口中更改它,在“ start in" 输入字段(在目标下方),或使用 python 方法os.getcwd()查找当前工作目录并os.chdir(path)设置它

Create an single file exe using PyInstaller and use Inno Setup to build the windows installer package.使用 PyInstaller 创建单个文件 exe 并使用 Inno Setup 构建 Windows 安装程序包。 Inno Setup will do the icon stuff for you. Inno Setup 会为你做图标的东西。

add --icon=iconname.ico添加--icon=iconname.ico
to the pyinstaller command in the prompt到提示符中的 pyinstaller 命令

eg>> pyinstaller --windowed --add-data "pics/myicon.ico;pics" --add-data "pics/*.png;pics" --icon=pics/myicon.ico -d bootloader myscript.py例如>> pyinstaller --windowed --add-data "pics/myicon.ico;pics" --add-data "pics/*.png;pics" --icon=pics/myicon.ico -d bootloader myscript.py

this will show your icon on the windows taskbar instead of the default python pkg icon这将在 Windows 任务栏上显示您的图标,而不是默认的 python pkg 图标

For those arriving at this post looking for a more accurate and complete answer:对于那些在这篇文章中寻找更准确和完整答案的人:

import tkinter as tk
root = tk.Tk()

root.iconbitmap('/path/to/ico/icon.ico')
root.mainloop()

Source reference is delftstack.com来源参考是delftstack.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM