Search code examples
pythonkivywidget

How to delete a widget in python kivy


I wanted to remove the 'bl' widget by clicking the 'horny mode' button, but it is not initially on the screen. How can I make the widget be removing by clicking on the button?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.uix.widget import Widget

class ScraperApp(App, FloatLayout):
    def PhotGif(self):
        wentil = Image(source='img.gif', size_hint = (.5, .5), anim_loop = 99999)
        photo = AnchorLayout(anchor_x='center', anchor_y='top', padding = [0, 75, 0, 0])
        photo.add_widget(wentil)
        return photo

    def build(self):
        
        bl = BoxLayout(orientation='horizontal', padding = [50, 100, 50, 150], spacing = 5)
        bl.add_widget( Button(text = '1', on_press = self.first, font_size = 20, size_hint = (.3, .1)))
        bl.add_widget( Button(text = '2', on_press = self.second, font_size = 20, size_hint = (.3, .1)))
        #bl.add_widget( Button(text = 'Wallpaper mode', on_press = self.wallpaper, font_size = 20, size_hint = (.3, .1)))
        wid = FloatLayout()
        wid.add_widget(ScraperApp().PhotGif())
        wid.add_widget(bl)
        return wid

    def first(self, instance):
        print('Horny mode')
        instance.text = 'кнопка нажата'
        ScraperApp().build().remove_widget(bl)
        stop()

    def stop():
        ScraperApp().build().remove_widget(bl)

    def second(self, instance):
        print('Soft mode')
        instance.text = 'кнопка нажата'

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

Can you help me make so that when you click on any button, the widget with the buttons removed?


Solution

  • First, save a reference to the widget that you want to remove:

    def build(self):
        self.bl = BoxLayout(orientation='horizontal', padding=[50, 100, 50, 150], spacing=5)
        self.bl.add_widget(Button(text='1', on_press=self.first, font_size=20, size_hint=(.3, .1)))
        self.bl.add_widget(Button(text='2', on_press=self.second, font_size=20, size_hint=(.3, .1)))
        # self.bl.add_widget( Button(text = 'Wallpaper mode', on_press = self.wallpaper, font_size = 20, size_hint = (.3, .1)))
        wid = FloatLayout()
        wid.add_widget(ScraperApp().PhotGif())
        wid.add_widget(self.bl)
        return wid
    

    Then modify the first() method to remove it:

    def first(self, instance):
        print('Horny mode')
        instance.text = 'кнопка нажата'
        self.root.remove_widget(self.bl)
        self.stop()
    

    Note that the code:

    ScraperApp().build().remove_widget(bl)
    

    creates a new instance of the ScaperApp and tried to remove bl from that instance. Aside from other problems, nothing you do to that new instance will affect the instance that is currntly running.