473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Lost in the inheritance tree...

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
Jul 19 '05 #1
3 1941
Adam Munoz Lopez wrote:
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....
Jul 19 '05 #2
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.

Jul 19 '05 #3
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)

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: zippy747 | last post by:
I would like to have a base class that does some work and can be instantiated. class a { public: virtual int foo() { return 0; }; virtual int bar() { return 0; }; };
6
by: Brendan Jurd | last post by:
Hi all, I read on the manual page for Inheritance that: "A limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
1
by: Zachary Hartnett | last post by:
I was trying to write a routine this morning that would open a given assembly, walk the inheritance tree of classes in the assembly, and provide a list of classes in the assembly that inherit from...
0
by: Dailan | last post by:
Hi, I create a tree view. Each node has link button associate with it, which includes add, edit, delete buttons. The way I did is for users who have very low capablity of using computer. Now I...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
1
by: Erik van Zijst | last post by:
I'm having trouble translating my object inheritance models to xmlschema and have illustrated this in the attached example xsd and xml. In my example I have a canvas for drawing a new painting...
6
by: Manan | last post by:
Hi, i'm new to OOP concept. in college days i have read lots of good things about "inheritance" but does any one uses this concept in real life project ? or can some one tell me when it will be...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.