python-设置窗口图标
当我尝试使用以下代码将左上角的窗口图标从难看的红色“ TK”更改为我自己的收藏夹图标时,Python抛出错误:
from tkinter import *
root = Tk()
#some buttons, widgets, a lot of stuff
root.iconbitmap('favicon.ico')
这应该将图标设置为“ favicon.ico”(根据网络上的许多论坛帖子)。 但是不幸的是,这行代码只会引发以下错误:
Traceback (most recent call last):
File "d:\ladvclient\mainapp.py", line 85, in
root.iconbitmap(bitmap='favicon.ico')
File "C:\Python33\lib\tkinter\__init__.py", line 1637, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "favicon.ico" not defined
我已经做了:
我检查了路径-一切都是正确的100%
我尝试了其他文件格式,例如py2exe或sth-无用
我在许多网站上查询了这个问题
第三点,我最喜欢的Tkinter网站effbot.org告诉我,Windows忽略了py2exe函数。但这并不能解释为什么会引发错误!
有一些避免这种问题的“骇客”方法,但是它们都不是针对Python 3.x编写的。
所以我的最后一个问题是:是否可以使用Python 3.x和Tkinter获得自定义图标?
另外,不要告诉我应该使用另一个GUI库。 我希望我的程序可以在每个平台上运行。 我也想要一个编码版本,而不是py2exe或sth解决方案。
9个解决方案
51 votes
您的脚本所在的文件夹或路径中不得包含favicon.ico。 输入完整的路径名。 例如,这有效:
from tkinter import *
root = Tk()
root.iconbitmap(r'c:\Python32\DLLs\py.ico')
root.mainloop()
但这会因您的错误而爆炸:
from tkinter import *
root = Tk()
root.iconbitmap('py.ico')
root.mainloop()
iCodez answered 2020-07-18T03:07:18Z
19 votes
这里建议的方法行不通-始终存在错误“未定义位图xxx”。 是的,我设置了正确的路径。
它的工作原理是:
imgicon = PhotoImage(file=os.path.join(sp,'myicon.gif'))
root.tk.call('wm', 'iconphoto', root._w, imgicon)
其中sp是脚本路径,而root是Tk根窗口。
很难理解它是如何工作的(我从fedoraforums无耻地复制了它),但是它起作用了
alessandro answered 2020-07-18T03:07:52Z
5 votes
这适用于Linux上的Python3:
import tkinter as tk
# Create Tk window
root = tk.Tk()
# Add icon from GIF file where my GIF is called 'icon.gif' and
# is in the same directory as this .py file
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(file='icon.gif'))
Steve Daulton answered 2020-07-18T03:08:11Z
1 votes
#!/usr/bin/env python
import tkinter as tk
class AppName(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = tk.Button(self, text='Quit', command=self.quit)
self.quitButton.grid()
app = AppName()
app.master.title('Title here ...!')
app.master.iconbitmap('icon.ico')
app.mainloop()
它应该像这样工作!
Malek B. answered 2020-07-18T03:08:32Z
1 votes
两种代码在python 3.7上都可以正常工作.....希望也能对您有效
import tkinter as tk
m=tk.Tk()
m.iconbitmap("myfavicon.ico")
m.title("SALAH Tutorials")
m.mainloop()
并且不要忘记将“ myfavicon.ico”保留在项目脚本文件所在的文件夹中
另一种方法
from tkinter import *
m=Tk()
m.iconbitmap("myfavicon.ico")
m.title("SALAH Tutorials")
m.mainloop()
[*注意:-python版本3适用于tkinter,低于版本3,即版本2适用于Tkinter]
Er M S Dandyan answered 2020-07-18T03:09:05Z
1 votes
也被困在那...
最终设法使用以下代码设置了我想要的图标:
from tkinter import *
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file='resources/icon.png'))
Yuval Farkash answered 2020-07-18T03:09:29Z
0 votes
确保.ico文件也未损坏。 尝试其他.ico文件时,我遇到的错误消失了。
Daniel answered 2020-07-18T03:09:49Z
0 votes
因此,看起来root.iconbitmap()仅支持固定目录。
sys.argv[0]返回从中读取文件的目录,因此使用简单的代码即可创建固定目录。
import sys
def get_dir(src):
dir = sys.argv[0]
dir = dir.split('/')
dir.pop(-1)
dir = '/'.join(dir)
dir = dir+'/'+src
return dir
这是输出
>>> get_dir('test.txt')
'C:/Users/Josua/Desktop/test.txt'
编辑:
唯一的问题是此方法在Linux上不起作用
josua@raspberrypi:~ $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv[0]
''
>>>
Josua Robson answered 2020-07-18T03:10:26Z
0 votes
from tkinter import *
from PIL import ImageTk, Image
Tk.call('wm', 'iconphoto', Tk._w, ImageTk.PhotoImage(Image.open('./resources/favicon.ico')))
以上为我工作。
Tarun Kolla answered 2020-07-18T03:10:46Z
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)