Inherit from Tkinter.Canvas - calling super leads to error Inherit from Tkinter.Canvas - calling super leads to error tkinter tkinter

Inherit from Tkinter.Canvas - calling super leads to error


The problem here is that TkInter classes (in 2.x) are old-style classes. The differences are described in detail in the Data Model documentation, but you don't need to know the details for this; all you need to know is that you can't use super (and how to work around that by explicitly calling __init__). And, as Steven Rumbalski points out, super() fails with error: TypeError “argument 1 must be type, not classobj” explains why you get this error message when the base class you're trying to super to is on old-style class.

This isn't mentioned in the documentation, and it's not really obvious unless you go looking for it, but if you know how to distinguish the two, it's not that hard.

As pointed out in a thread on python-list, this usually isn't a problem, because if you need new-style class behavior, you can always just do class CCanvas(tk.Canvas, object):.

But there are a few things that doesn't take care of, and one of them is the ability to super to the base class. Instead, you have to do things the old-fashioned way and refer to the base class explicitly by name (which also means you have to pass self explicitly):

def __init__(self,master,*args,**kwargs):    TkInter.Canvas.__init__(self, master=master, *args, **kwargs)

(Of course another solution is to migrate to Python 3 already, where there are no old-style classes…)

(For the sake of completeness, it is possible to fake 90% of super with old-style classes, and there were some recipes floating around back in the early 2.x days… but you don't want to do that.)