Skip to content

Commit

Permalink
Merge pull request #585 from kivy/add__widget
Browse files Browse the repository at this point in the history
UIX: Widget: Raise Exception if widget already has parent.
  • Loading branch information
tito committed Jul 24, 2012
2 parents c529686 + 33ba4a3 commit 75445ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kivy/uix/bubble.py
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions kivy/uix/tabbedpanel.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions kivy/uix/widget.py
Expand Up @@ -226,6 +226,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('Cannot add %r, it already has a parent %r'
% (widget, parent))
widget.parent = self
if index == 0 or len(self.children) == 0:
self.children.insert(0, widget)
Expand Down

0 comments on commit 75445ea

Please sign in to comment.