2016-12-08 148 views
0

我想改变我的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__(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() 

抛出NameError: name 'root' is not defined。我误解了什么?

+2

错误究竟是出了什么问题告诉你。你已经定义了'self.root'而不是'root'。 –

回答

4

您需要通过self.root

改变访问root

self.root.tk.call('wm', 'iconphoto', root._w, img) 

到:

self.root.tk.call('wm', 'iconphoto', self.root._w, img) 
+0

哇就是这样。非常感谢!我现在觉得很愚蠢。 –

+1

没问题,习惯面向对象编程需要时间。 –

+0

有没有办法避免访问'root'的私有'_w'属性? – phoenix