![](/img/trans.png)
[英]TKinter: How can I set the window icon of a child window (Toplevel)
[英]tkinter - replace icon for child window
我想替换顶级窗口和子窗口的默认 python tkinter 图标。 我尝试了几种导致错误的方法。 替换父窗口和子窗口图标的最佳方法是什么? 请参阅下面的尝试 - 我正在使用 python 3.9.1
import tkinter as tk
from PIL import Image, ImageTk
m = tk.Tk()
m.title('Main Window')
m.geometry('300x100')
blankImageObject = tk.PhotoImage(file='icons/blankIcon.png')
m.tk.call('wm', 'iconphoto', m._w, blankImageObject)
c = tk.Tk('m')
c.title('Child Window')
c.geometry('300x100')
#try to re-use the same image object created for the main window
#c.tk.call('wm', 'iconphoto', c._w, blankImageObject)
#try creating a new PhotoImage object
#c.tk.call('wm', 'iconphoto', c._w, tk.PhotoImage(file='icons/blankIcon.png'))
#try using main window instead of the child window in the Tcl call
#c.tk.call('wm', 'iconphoto', m._w, tk.PhotoImage(file='icons/blankIcon.png'))
#try using main window in the Tcl call and the blankImageObject from the main window
#c.tk.call('wm', 'iconphoto', m._w, blankImageObject)
m.mainloop()
您可以通过创建一个新的PhotoImage
对象来实现,但是您需要在这样做时通过master=
关键字参数指定正确的Tk
实例,如下图所示:
我还建议您阅读为什么不鼓励使用多个 Tk 实例?
import tkinter as tk
from PIL import Image, ImageTk
icon_filepath='icons/blankIcon.png'
m = tk.Tk()
m.title('Main Window')
m.geometry('300x100')
blankImageObject = tk.PhotoImage(file=icon_filepath)
m.tk.call('wm', 'iconphoto', m._w, blankImageObject)
c = tk.Tk('m')
c.title('Child Window')
c.geometry('300x100')
# Create a new PhotoImage object
blankImageObject2 = tk.PhotoImage(master=c, file=icon_filepath)
c.tk.call('wm', 'iconphoto', c._w, blankImageObject2)
m.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.