Kivy Popup Widget -already has a parent widget error

2,465 views
Skip to first unread message

bhavar kumavat

unread,
Mar 3, 2016, 10:44:08 PM3/3/16
to Kivy users support

Here is the Code I am on.
Popup works for the first time when the Button '1' is clicked but If I click again on the button I get an error
'kivy.uix.widget.WidgetException: Cannot add <__main__.ButtonOne object at 0x03B04AE8>, it already has a parent <kivy.uix.boxlayout.BoxLayout object at 0x068CD228>'

I don't know why is this happening anyone could explain me this,It would be appreciable.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
Builder.load_string('''
<MyApp>:
BoxLayout:
orientation:'horizontal'
GridLayout:
cols:1
padding:20
spacing:20
TextInput:
id:search_key
size_hint:1,None
height:50
multiline:False
spacing:20
on_text_validate:root.d()
Button:

BoxLayout:
orientation:'vertical'
ScrollView:
size_hint:1,1
GridLayout:
size_hint:1,None
height:self.minimum_height
cols:1
BoxLayout:
size_hint:1,None
height:500
orientation:'vertical'
Button:
size_hint:1,.7
			    text:'1'
on_press:app.get_image()
GridLayout:
size_hint:1,.3
cols:3
Button:
Button:
Button:
Button:
size_hint:1,None
height:500

Button:
size_hint:1,None
height:500
''')

class ButtonOne(Button):
pass

class CustomPopup(Popup):
from kivy.uix.button import Button
b=ButtonOne()
def __init__(self,**kwargs):
super(CustomPopup,self).__init__(**kwargs)
self.size_hint=[.5,.5]
self.auto_dismiss=False
self.title='That\'s the title'
self.b.text="Just a button"
self.b.bind(on_release=self.dism)
self.content=self.b
def dism(self,*args):
self.dismiss()

class MyApp(BoxLayout):
def d(self):
print 'lost focus'
pass

class Main(App):
def build(self):
return MyApp()

def get_image(self):
c=CustomPopup()
c.open()

Main().run()

OPQ

unread,
Mar 4, 2016, 4:35:12 AM3/4/16
to Kivy users support
Indedd, as the message says: the first time you click, you attached the class-linked ButtonOne to a first popup. 
The second time you click;, you attached the SAME buttonOne to a new popup.
A widget child can not be moved around. It has to be detached from its parent, before being added to a new parent.

As such, you can either:
- create a new buttonOne each time you click, which is probably the desired behavior (replace self.content = self.b by self.content = self.b = ButtonOne())
- or, if you really need to keep the same button around, you'll have to untach it from its parent: priori to self.content = self.b, add a new line with  self.b.parent.remove_widget(self.b)


--OPQ
Reply all
Reply to author
Forward
0 new messages