[Tutor] Class Inheritance

David Hutto smokefloat at gmail.com
Fri Apr 23 07:11:36 CEST 2010


Hello List!

While experimenting with Tkinter(python2.6), when from Tkinter import*
is used I came across the following error:
************
C:\Users\ascent>c:\python26/Script3.py
Traceback (most recent call last):
  File "C:\python26\Script3.py", line 1, in <module>
    from Tkinter import *
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in <module>
    from turtle import *
  File "C:\Python26\lib\lib-tk\turtle.py", line 374, in <module>
    class ScrolledCanvas(Tkinter.Frame):
AttributeError: 'module' object has no attribute 'Frame'

*************

Which stems from the below in turtle.py:

class ScrolledCanvas(TK.Frame)

I know that ScrolledCanvas is trying to use class TK.Frame as it's base
class to build from, and the class Frame is what is trying to be called
from the Tkinter module.

So I tried to alter the turtle.py. When I try to just 'from Tkinter
import *, such as:

from Tkinter import *
class ScrolledCanvas(Tkinter.Frame):

I get:
*****************
C:\Users\ascent>c:\python26/Script3.py
Traceback (most recent call last):
  File "C:\python26\Script3.py", line 1, in <module>
    from Tkinter import *
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in <module>
    from turtle import *
  File "C:\Python26\lib\lib-tk\turtle.py", line 373, in <module>
    class ScrolledCanvas(Tkinter.Frame):
NameError: name 'Tkinter' is not defined
*******************


I know pretty much what is going on there. But when I try to use:

import Tkinter
from Tkinter import *
class ScrolledCanvas(Tkinter.Frame):

It takes me back to the first error. Which means
in both instances both directly called by me, and
when called from the original turtle.py call,
it's not finding the Frame class.

>From the docs (9.5. Inheritance) it states:

"The name BaseClassName must be defined in a
scope containing the derived class definition.
In place of a base class name, other arbitrary
expressions are also allowed. This can be useful,
for example, when the base class is defined in another module:

class DerivedClassName(modname.BaseClassName)
"


So why does the above, from turtle.py, a standard module,
not allow this, or is their something
the module writer got wrong, or more likely, that I'm not
understanding about what it's doing?

As a sidenote, I ended up removing the from turtle import *
line from Tkinter which resolved the problem(the example I was using
didn't have a canvas, and I'm pretty sure Tkinter was defaulting
to the ScrolledCanvas).


TIA,
David


More information about the Tutor mailing list