Search code examples
pythonkivy

how to use remove_widget?


i tried to make a simple program but i have problems with remove_widget. Also i checked many posts and sites but i couldn't find answer

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.lang import Builder

class FirstScr(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        main_line = BoxLayout(orientation = 'vertical', padding=8, spacing=8)
        go = Label(text='Нажми на нужную кнопку!', halign='center')
        self.btn = Button(text='Жми!', size_hint=(.3,.2), pos_hint={'center_x':.5})
        self.btn2 = Button(text='Не жми сюда!', size_hint=(.3,.2), pos_hint={'center_x':.5})

        main_line.add_widget(go)
        main_line.add_widget(self.btn)
        main_line.add_widget(self.btn2)

        self.add_widget(main_line)

        self.btn.on_press = self.next
        self.btn2.on_press = self.next2
    def next(self):
        main_line = BoxLayout(orientation = 'vertical', padding=8, spacing=8)
        go2 = Label(text='Молодец!')

        main_line.add_widget(go2)
        
        self.add_widget(main_line)
    def next2(self, **kwargs):
        super().__init__(**kwargs)
        self.remove_widget(self.btn2)
class MyApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(FirstScr(name='first'))

        return sm
MyApp().run()

Can u help me guys?

I tried to change location of function but it isn't working


Solution

  • It seems like next2 calls super for no reason before removing the widget.

    if your aim is to remove btn2 you should remove it from the layout :

    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.uix.textinput import TextInput
    from kivy.lang import Builder
    
    class FirstScr(Screen):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            main_line = BoxLayout(orientation='vertical', padding=8, spacing=8)
            go = Label(text='Click the necessary button!', halign='center')
            self.btn = Button(text='Press!', size_hint=(.3, .2), pos_hint={'center_x': .5})
            self.btn2 = Button(text="Don't press here!", size_hint=(.3, .2), pos_hint={'center_x': .5})
    
            main_line.add_widget(go)
            main_line.add_widget(self.btn)
            main_line.add_widget(self.btn2)
    
            self.add_widget(main_line)
    
            self.btn.on_press = self.next
            self.btn2.on_press = self.next2
    
        def next(self):
            main_line = BoxLayout(orientation='vertical', padding=8, spacing=8)
            go2 = Label(text='Well done!')
    
            main_line.add_widget(go2)
    
            self.add_widget(main_line)
    
        def next2(self):
            # Remove btn2 from the layout (main_line)
            self.main_line.remove_widget(self.btn2)
    
    class MyApp(App):
        def build(self):
            sm = ScreenManager()
            sm.add_widget(FirstScr(name='first'))
    
            return sm
    
    MyApp().run()