Skip to content

UIX: Widget: Raise Exception if widget already has parent. #585

Merged
merged 3 commits into from Jul 24, 2012

2 participants

@akshayaurora
Kivy member

Along with fixes in TabbedPanel and Bubble for issues due to this
warning.

@akshayaurora akshayaurora UIX: Widget: Warn and skip addition if widget already has parent.
Along with fixes in TabbedPanel and Bubble for issues due to this
warning.
e49a29f
@akshayaurora akshayaurora and 1 other commented on an outdated diff Jul 20, 2012
kivy/uix/widget.py
@@ -227,6 +228,13 @@ def add_widget(self, widget, index=0):
if not isinstance(widget, Widget):
raise WidgetException(
'add_widget() can be used only with Widget classes.')
+ parent = widget.parent
+ # check if widget is already a child of another widget
+ if parent:
+ Logger.warning(''.join(('Cannot add Widget ', str(widget),\
+ ' It already has a parent ', str(parent),
+ ' Skipping addition')))
+ return
@akshayaurora
Kivy member
akshayaurora added a note Jul 20, 2012

This would highlight the issue, instead of just silently fixing it but could this break existing
code for people? Should we just raise a warning and add the widget any ways?

@tito
Kivy member
tito added a note Jul 20, 2012

Then, i would prefer an exception than a warning ? What do you think ?
I agreed that this shouldn't be the case, and we could fail correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tito tito commented on the diff Jul 21, 2012
kivy/uix/bubble.py
@@ -157,6 +157,7 @@ def __init__(self, **kwargs):
color=self.background_color)
self.content = content = BubbleContent(parent=self)
super(Bubble, self).__init__(**kwargs)
+ content.parent = None
@tito
Kivy member
tito added a note Jul 21, 2012

Is the initialization is really needed ? It shouldn't be, remove it.

@akshayaurora
Kivy member
akshayaurora added a note Jul 21, 2012

At line 158 the BubbleContent's parent is being set to self. which prevents the addition of the content to the bubble itself because the parent already exists.
Update: I think that(parent=self) part is needed because of this c0997c8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tito tito commented on an outdated diff Jul 21, 2012
kivy/uix/widget.py
@@ -227,6 +227,11 @@ def add_widget(self, widget, index=0):
if not isinstance(widget, Widget):
raise WidgetException(
'add_widget() can be used only with Widget classes.')
+ parent = widget.parent
+ # check if widget is already a child of another widget
+ if parent:
+ raise WidgetException(''.join(('Cannot add ', str(widget),\
@tito
Kivy member
tito added a note Jul 21, 2012

I didn't seen the writing before, but don't use ''.join() for formatting, use the string / repr like that:

raise WidgetException('Cannot add %r, it already has a parent %r' % (
    widget, parent))
@tito
Kivy member
tito added a note Jul 21, 2012

%r = repr(arg)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tito tito merged commit 75445ea into master Jul 24, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.