Python 设置窗口图标tkinter
我正在尝试更改tkinter窗口上的图标,我认为我的问题源于我对类的理解不足 我想知道为什么:Python 设置窗口图标tkinter,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在尝试更改tkinter窗口上的图标,我认为我的问题源于我对类的理解不足 我想知道为什么: import tkinter root = tkinter.Tk() img = tkinter.PhotoImage(file = r'stockIcon.gif') root.tk.call('wm', 'iconphoto', root._w, img) root.mainloop() 工作完美。但是: import tkinter class Test: def __init__
import tkinter
root = tkinter.Tk()
img = tkinter.PhotoImage(file = r'stockIcon.gif')
root.tk.call('wm', 'iconphoto', root._w, img)
root.mainloop()
工作完美。但是:
import tkinter
class Test:
def __init__(self):
self.root = tkinter.Tk()
self.img = tkinter.PhotoImage(file = r'stockIcon.gif')
self.root.tk.call('wm', 'iconphoto', root._w, img)
self.root.mainloop()
test = Test()
抛出
name错误:未定义名称“root”
。我误解了什么?您需要通过self.root
更改:
self.root.tk.call('wm', 'iconphoto', root._w, img)
致:
哇,就是这样。非常感谢你!我现在觉得自己很笨。没问题,习惯面向对象编程需要时间。有没有办法避免访问
root
的私有\u w
属性?@phoenix是:str(self.root)
。甚至可能只是self.root
astkinter
将传递给Tk/Tcl的所有内容转换成字符串,因为Tcl是一种非常“严格类型”的语言。:-)错误是告诉您问题的确切原因。您已经定义了self.root
,但没有定义root
。
self.root.tk.call('wm', 'iconphoto', self.root._w, img)