Search code examples
pythonkivykivy-language

Python Kivy library adding widgets


For example we created with kivy language simple BoxLayout1 that contains button1 and another BoxLayout2. When you click button1 it adds a new button in BoxLayout2 but it's written in python.

Is it possible to acces existing layout in kv language, in python code? Or only solution is writting whole window in python?

I couldn't find any info in kivy docs, maybe I've just missed something.

EDIT:

I've something like this

Kv:

<CreateWindow>:
    BoxLayout:
        Button:
              text: "test"
              on_release: root.press()
    BoxLayout:
        Label:
              text: "Label"

Python:

class CreateWindow(Screen):
      def press(self):

I want to add a new button near Label by activating press function


Solution

  • in python will be like this

    
    class CreateWindow(Screen):
          def press(self):
              # the first box is the first child in the children list of the CreateWindow widget so
              box1=self.children[0]
              box2=self.children[1]
              # and now you can decide which box you want to use add_widget method with
              # in your case it should be
              box2.add_widget(Button())