Python:Tkinter iconbitmap 分配仅适用于模块级别 [英] Python: Tkinter iconbitmap assignment only works at module level

查看:52
本文介绍了Python:Tkinter iconbitmap 分配仅适用于模块级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 tkinter.ttk 窗口,并且我正在使用一个图标来设置我的窗口的图标位图.但是 root.iconbitmap() 在 Windows 10 上被忽略.但是有一个简单的方法可以避免错误:root.tkinter.call('wm', 'iconphoto', root._w, 图标)

I'm using a tkinter.ttk window and I'm using an icon to set the iconbitmap of my window. However root.iconbitmap() is ignored on Windows 10. But There is an easy way to avoid an error: root.tkinter.call('wm', 'iconphoto', root._w, icon)

所以:

from tkinter import *
from tkinter.ttk import *

root=Tk()
root.call('wm', 'iconphoto', root._w, icon)

有效.但是:

def func():
    root=Tk()
    root.call('wm', 'iconphoto', root._w, icon)

不起作用.发生错误.有趣的是,该错误与使用 root.iconbitmap() 时发生的错误完全相同:

does NOT work. An error occurs. It's interesting that that error is exactly the same one that occurs when you use root.iconbitmap():

Traceback (most recent call last):
File "E:\test.py", line 95, in <module>
func()
File "E:\test.py", line 36, in func
t.call('wm', 'iconphoto', t._w, icon)
_tkinter.TclError: can't use "pyimagex" as iconphoto: not a photo Image

还有一个有趣的事实:在另一个文件中,我也尝试将它用作函数,它奏效了.在新文件 (test.py) 中它不起作用(它是相同的功能).有谁知道为什么它不起作用以及我可以做些什么来避免错误?提前致谢...

And there is one interesting fact left: In another file I tried to use it as a function too, it worked. In the new file (test.py) it didn't work (and it was the same function). Does anybody know why it doesn't work and what I can do to avoid an error? Thanks in advance...

推荐答案

如果你已经打开了一个窗口并且想要打开另一个带有它自己图标的窗口,那么你应该使用 Toplevel() 代替Tk() 并更改图标使用

If you've a window opened already and wants to open another one with it's own icon then you should use Toplevel() instead of Tk() and to change the icon use

W2 = Toplevel()
icon = PhotoImage(file='icon.png')
W2.tk.call('wm', 'iconphoto', root._w, icon)

示例:

from tkinter import *
from tkinter.ttk import *

def test():
    root = Toplevel()
    icon = PhotoImage( file='icon.png' )  # path to the icon
    root.tk.call('wm', 'iconphoto', root._w, icon)

r = Tk()

b =  Button(r, text='press', command=test)
b.pack()

mainloop()

这篇关于Python:Tkinter iconbitmap 分配仅适用于模块级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆