Kogan007
Kogan007

Reputation: 164

Kivy Add Widget Error

my problem is that when I want to run the example above, it will not work. What happens is, the popup shows up once with the text "test" and then when I click the button again it shows the errors:

File "/home/river/android-sdk/workplace/python-forandroid/build/pythoninstall/lib/python2.7/site-packages/kivy/uix/layout.py", line 76, in add_widget
File "/home/river/android-sdk/workplace/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/uix/widget.py", line 314, in add_widget
kivy.uix.widget.WidgetException: Cannot add <kivy.uix.boxlayout.BoxLayout object at 0x60a25a78>, it already has a parent <kivy.uix.boxlayout.BoxLayout object at 0x60e2bae8>

I think the reason being is that I have to remove the widget after each click. (I dont know how to)

 import kivy
 import kivy.uix.label
 import kivy.app
 import kivy.lang.builder
 textshow = BoxLayout()
 def answer(answer):
      text = Label(text=str(answer))
      textshow.add_widget(text)
 Builder.load_string('''
 <main>:
     Button: 
         on_release: root.show()
 ''')
 class main(BoxLayout):
      def show(self):
           answer("test")
           popup = Popup(content=textshow)
           popup.open()
 class apprun(App):
     def build(self):
         return main()
 apprun().run()

If you answer this, please show whole code.

Edit: This is a sample script, my real script has 2 class Screen1, and textshow = BoxLayout() class

Inclements answer worked, but every click of the button makes another textshow! Please help!

Upvotes: 1

Views: 1778

Answers (1)

inclement
inclement

Reputation: 29450

A simple answer would be to store the popup and simply use the smae on each time. For instance, you could put self.popup = Popup(content=textshow) in your build method then change your show method to have App.get_running_app().popup.open(). This will always open the single saved popup, avoiding the problem.

Upvotes: 3

Related Questions