Permalink
Browse files

UIX: Widget: Warn and skip addition if widget already has parent.

Along with fixes in TabbedPanel and Bubble for issues due to this
warning.
  • Loading branch information...
1 parent 422dc30 commit e49a29f1e9b3f137bc4570b1cfad69022e0bf448 @akshayaurora akshayaurora committed Jul 15, 2012
Showing with 19 additions and 0 deletions.
  1. +5 −0 kivy/uix/bubble.py
  2. +6 −0 kivy/uix/tabbedpanel.py
  3. +8 −0 kivy/uix/widget.py
View
@@ -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
self.add_widget(content)
self.on_arrow_pos()
@@ -226,6 +227,10 @@ def on_arrow_pos(self, *l):
self_arrow_img.height = self_arrow_img.texture_size[1]
widget_list = []
arrow_list = []
+ parent = self_arrow_img.parent
+ if parent:
+ parent.remove_widget(self_arrow_img)
+
if self_arrow_pos[0] == 'b' or self_arrow_pos[0] == 't':
self.cols = 1
self.rows = 2
View
@@ -393,6 +393,9 @@ def add_widget(self, widget, index=0):
content = self.content
if content is None:
return
+ parent = widget.parent
+ if widget.parent:
+ parent.remove_widget(widget)
if widget == content or widget == self._tab_layout:
super(TabbedPanel, self).add_widget(widget, index)
elif isinstance(widget, TabbedPanelHeader):
@@ -491,6 +494,9 @@ def on_tab_pos(self, *l):
tab_layout.clear_widgets()
scrl_v = ScrollView(size_hint=(None, 1))
tabs = self._tab_strip
+ parent = tabs.parent
+ if parent:
+ parent.remove_widget(tabs)
scrl_v.add_widget(tabs)
scrl_v.pos = (0, 0)
self_update_scrollview = self._update_scrollview
View
@@ -65,6 +65,7 @@ def callback_pos(instance, value):
from kivy.graphics import Canvas
from kivy.base import EventLoop
from kivy.lang import Builder
+from kivy.logger import Logger
class WidgetException(Exception):
@@ -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
widget.parent = self
if index == 0 or len(self.children) == 0:
self.children.insert(0, widget)

0 comments on commit e49a29f

Please sign in to comment.