How can I set a tkinter app icon with a URL?

依然范特西╮ 提交于 2019-12-11 07:18:54

问题


If I have:

from Tkinter import *
app = Tk()

...

app.mainloop()

Would I use app.iconbitmap(...)?

And if so, how would I go about using this as the file, and should I import urllib?


回答1:


You can use this too replace the Tkinter default icon.

import base64, PIL, urllib, Tkinter
from Tkinter import *
from PIL import ImageTk
from urllib import *

root = Tk()

raw_data = urllib.urlopen("http://dl.dropboxusercontent.com/s/qtlincxkbbiz1qv/stat.gif").read()

b64_data = base64.encodestring(raw_data)
image = PhotoImage(data=b64_data)

root.tk.call('wm', 'iconphoto', root._w, image)

root.mainloop()

And then change the .py file extension to .pyw to change the taskbar icon.

The .pyw extension tells it to run with pythonw.exe instead of python.exe, but running with pythonw.exe also makes it run without the console.

So, you'll either have to run without the icon, or without the console.




回答2:


This is the call that worked for me on both Windows and Linux. I found that I cannot use ico files on Linux, so only using gif files which works on both platforms.

class Editor(tk.Tk):
   . . .
   . . . 
   self.tk.call('wm', 'iconphoto', self._w, tk.PhotoImage(file = "my_icon.gif"))


来源:https://stackoverflow.com/questions/22133696/how-can-i-set-a-tkinter-app-icon-with-a-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!