Why can't I delete an method/attribute from a Tkinter Window?
I'm trying to remove a method from an class instance derived from a Tkinter window using the delattr
built in function. However, I get the following error. What am I doing wrong?
Error :
AttributeError: Class instance has no attribute 'wm_title'
An example :
import Tkinter as tk
class Class (tk.Tk) :
def __init__ (self) :
tk.Tk.__init__(self)
# The method is clearly there, seeing as this works.
self.wm_title('')
# This raises an AttributeError.
delatt开发者_如何学Cr(self, 'wm_title')
c = Class()
c.mainloop()
You can't delete a class method that way because class methods are properties of classes, not objects.
When you invoke a method via object.method()
, python is actually calling Class.method(object)
. (This is also why you must declare a self
argument in class methods, yet you do not actually pass any value for self
when invoking that method.)
If you want, you could call del Class.wm_title
. (I'm not sure why you want to, though.)
精彩评论