设置窗口图标tkinter

时间:2016-12-08 18:04:21

标签: 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__(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。我误解了什么?

1 个答案:

答案 0 :(得分:5)

您需要通过root

访问self.root

变化:

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

为:

self.root.tk.call('wm', 'iconphoto', self.root._w, img)
相关问题