Python 2.7 super () error

Trying to create a Tkinter window using the super () function. I get this error:

super (application, itself) ._ init_ (master) TypeError: must be a type, not classobj

the code:

import Tkinter as tk class Application(tk.Frame): def __init__(self, master): super(Application, self).__init__(master) self.grid() def main(): root = tk.Tk() root.geometry('200x150') app = Application(root) root.mainloop() main() 
+4
source share
2 answers

Tkinter uses old-style classes. super() can only be used with new-style classes .

+4
source

Although it is true that Tkinter uses old-style classes, this limitation can be overcome by further deriving the Application subclass from object (using Python multiple inheritance):

 import Tkinter as tk class Application(tk.Frame, object): def __init__(self, master): super(Application, self).__init__(master) self.grid() def main(): root = tk.Tk() root.geometry('200x150') app = Application(root) root.mainloop() main() 

This will work until the Tkinter class tries to execute any behavior that requires the use of the old-style class (which I highly doubt it). I tested the above example with Python 2.7.7 without any problems.

This work has been proposed here . This behavior is also enabled by default in Python 3 (link in link).

+8
source

Source: https://habr.com/ru/post/1496393/


All Articles