How to add and remove wigdets dynamically?

Asked

Viewed 214 times

1

import kivy
kivy.require('1.10.1')

from kivy.app import App  
from kivy.uix.boxlayout import BoxLayout  
from kivy.uix.button import Button  

class Tela-1 (BoxLayout):

    def on_press_bt(self):
        janela.root_window.remove_wigdet(janela.root)
        janela.root_window.add_wigdet(Tela2())

    def __init__(self, **kwargs):
        super(Tela1, self).__init__(**kwargs)
        self.orientation = 'vertical'
        bt1 = Button(text='Click')
        bt1.on_press = self.on_press_bt
        self.add_widget(bt1)
        self.add_widget(Button(text='bt2'))
        self.add_widget(Button(text='bt3'))

class Tela-2 (BoxLayout):

    def on_press_bt(self):
        janela.root_window.remove_wigdet(janela.root)
        janela.root_window.add_wigdet(Tela1())

    def __init__(self, **kwargs):
        super(Tela2, **kwargs).__init__(**kwargs)
        self.orientation = 'vertical'
        bt = Button(text='Click')
        bt.on_press = self.on_press_bt
        self.add_widget(bt)  

class Principal(App):


    def build(self):  
        return Tela1()
janela = Principal()  
janela.run()
  • The intention was: to add three buttons, and when it was pressed the button named 'Click' should switch, and the three buttons that were previously on the screen would disappear, leaving only one button..

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.