Lost in the inheritance tree...

  • Thread starter Adam Munoz Lopez
  • Start date
A

Adam Munoz Lopez

Can anyone help with this code... I have infinite
recursion but since I'm pretty new to Python (and
programming in general) I can't find where I did the
mistake.

Thanks a lot in advance.

Adam

*************************************************
import Tkinter
class RootFrame(Tkinter.Frame):
def
__init__(self,parent=None,myHeight=600,myWidth=800,myBd=3,\
myRelief=Tkinter.RIDGE):

Tkinter.Frame.__init__\

(self,parent,height=myHeight,width=myWidth,bd=myBd,\
relief=myRelief)

self.grid()
self.grid_propagate(0)

self.createFrames()
self.showMsg()

def showMsg(self):
msg=Tkinter.Label(textFrame,text="Are you
thinking of an animal?")
msg.grid()

def createFrames(self):
textFrame=TextFrame(self,300,600)
textFrame.grid()
textFrame.grid_propagate(0)

def createButtons(self):
yesButton=Tkinter.Button(self,text="Yes")
yesButton.grid(row=1,sticky=Tkinter.E)
noButton=Tkinter.Button(self,text="No")
noButton.grid(row=1,sticky=Tkinter.W)

class TextFrame(RootFrame):
def __init__(self,parent,myHeight,myWidth):

RootFrame.__init__(self,parent,myHeight,myWidth)


rootFrame=RootFrame()
rootFrame.mainloop()






___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com
 
R

Rocco Moretti

Adam said:
Can anyone help with this code... I have infinite
recursion but since I'm pretty new to Python (and
programming in general) I can't find where I did the
mistake.

It really does help to start removing things trying to get the minimal
code which causes the problem.

(untested snippage follows)
*************************************************
import Tkinter
class RootFrame(Tkinter.Frame):
def __init__(self,parent=None,myHeight=600,myWidth=800,myBd=3,\
myRelief=Tkinter.RIDGE):
self.createFrames()

def createFrames(self):
textFrame=TextFrame(self,300,600)


class TextFrame(RootFrame):
def __init__(self,parent,myHeight,myWidth):
RootFrame.__init__(self,parent,myHeight,myWidth)

rootFrame=RootFrame()

It appears you create a RootFrame, which creates a TextFrame, which
initilizes as a RootFrame, thus creating a TextFrame which initilizes as
a RootFrame, thus creating a TextFrame which....
 
J

Jordan Rastrick

Although you get infinite recursion with this code, you still get
enough information on the error from the interpreter to help you debug.

Running IDLE, I get a traceback of:

File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line 11,
in __init__
self.createFrames()
File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line
19, in createFrames
textFrame=TextFrame(self,300,600)
File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line
31, in __init__
RootFrame.__init__(self,parent,myHeight,myWidth)

repeated indefinitely. At a glance, this tells you:

* That __init__ (of the RootFrame method) calls self.createFrames()
* createFrames(), in turn, calls TextFrame(self,300,600)
* This leads to RootFrame.__init__ being called once more

So theres youre infinite recursion. RootFrame's __init__ calls
createFrames which creates a new TextFrame - meaning TextFrame.__init__
gets called, and this calls its parent's __init__ method, and so on ad
infinitum.

This is a pretty good demonstration of the prinicple that you should do
as little as is nessecary to create an object - if possible, try to
calling other methods on an object in its __init__ method.

Without knowing more about youre program, and with only limited GUI
building experience (and none in Python), I'd guess the most likely
solution is for TextFrame to inherit from Tkinter.Frame directly -
having it inheret from RootFrame doesn't really make much sense as far
as I can see. TextFrame is, I would guess, intended as a component of
RootFrame, not a subclass. Thats another important OO prinicple - don't
overuse inheritance, often simple composition (one object having
another as an attribute) is the right solution.

If TextFrame really is supposed to inherit from RootFrame, try to
explain why.
 
J

Jordan Rastrick

comp.lang.python is a great newsgroup in that respect - so long as you
ask a semi-intelligent question, you nearly always end up with a quick
and helpful response.

Good luck with learning programming, and Python (IMO its one of the
best possible languages to do it in)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top