Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 设置窗口图标tkinter_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 设置窗口图标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__

我正在尝试更改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()

抛出
name错误:未定义名称“root”
。我误解了什么?

您需要通过
self.root

更改:

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


哇,就是这样。非常感谢你!我现在觉得自己很笨。没问题,习惯面向对象编程需要时间。有没有办法避免访问
root
的私有
\u w
属性?@phoenix是:
str(self.root)
。甚至可能只是
self.root
as
tkinter
将传递给Tk/Tcl的所有内容转换成字符串,因为Tcl是一种非常“严格类型”的语言。:-)错误是告诉您问题的确切原因。您已经定义了
self.root
,但没有定义
root
self.root.tk.call('wm', 'iconphoto', self.root._w, img)