1

I wanted to ask a clever question about using Canvas as a container, but writing my example code I stumbled over something weird. Here is the code so far:

import Tkinter as tk

class CCanvas(tk.Canvas):

    def __init__(self,master,*args,**kwargs):
        super(CCanvas,self).__init__(master=master,*args,**kwargs)


if __name__ == '__main__':
    root= tk.Tk()
    cc = CCanvas(root)
    cc.pack()
    root.mainloop()

Now this code should not do much. The class CCanvas just inherits from Canvas, doesn't implement anything, just calls the constructor of the super-class. I don't see any reason for this not to work. Yet, when I run this, I get the following error:

super(CCanvas,self).__init__(master=master,*args,**kwargs)
TypeError: must be type, not classobj

Can anyone explain this behavior to me and maybe tell me how to fix it?

Dani Gehtdichnixan
  • 1,265
  • 1
  • 11
  • 21
  • possible duplicate of [super() fails with error: TypeError "argument 1 must be type, not classobj"](http://stackoverflow.com/questions/1713038/super-fails-with-error-typeerror-argument-1-must-be-type-not-classobj) – Steven Rumbalski Jan 17 '13 at 23:21
  • I'm not sure it's a dup, because the fact that TkInter classes aren't new-style classes is not exactly obvious, or documented anywhere, unlike the case where the base class is explicitly defined as `class B:` in the code. – abarnert Jan 17 '13 at 23:26
  • Tkinter classes are "old-style" classes. [You can't use `super`](http://stackoverflow.com/questions/3694371/how-do-i-initialize-the-base-super-class-in-python/3694393#3694393) with old-style classes – Steven Rumbalski Jan 17 '13 at 23:28
  • Jep, my bad. I previously extended FigureCanvasTkAgg from matplotlib (which is new style), and thought that it would work the same here. What is the right thing to do here, delete it because it is sort of a duplicate? – Dani Gehtdichnixan Jan 17 '13 at 23:43

1 Answers1

6

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.)

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671