Raka Wiratama
Raka Wiratama

Reputation: 35

how do i break a button into smaller buttons in kivy (grid layout) without changing screen

for example, here is the code

.py file

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

class ScreenOne(Screen):
   pass
class WindowManager(ScreenManager):
   pass

kv = Builder.load_file("testing.kv")

class ScreenApp(App):
    def build(self):
        return kv


if __name__ == '__main__':
   ScreenApp().run()

.kv vile

WindowManager:
    ScreenOne:

<ScreenOne>:
    GridLayout:
        size: root.height, root.width
        rows: 2
        Button:
            text: "Break this button into 3 smaller buttons"
        Label:
            text: ""

When the button is clicked, i wanted to break the button into 3 smaller buttons. Can you help me to make an on_release command to do it?

Upvotes: 0

Views: 78

Answers (1)

Amin Etesamian
Amin Etesamian

Reputation: 3699

<ScreenOne>:
    GridLayout:
        btn: remove
        size: root.height, root.width
        rows: 2
        Button:
            id: remove
            text: "Break this button into 3 smaller buttons"
            on_press: root.break_into_three()

then in your ScreenOne class create a function named break_into_three and define it as follows:

class ScreenOne(Screen):
   
    def break_into_three(self):
        self.add_widget(Button(text='Button1'))
        self.add_widget(Button(text='Button2'))
        self.add_widget(Button(text='Button3'))
        self.remove_widget(self.btn)  # removes the initial Button

I don't have a working Kivy environment to test this but I think you get the point.

Upvotes: 1

Related Questions