user48944
user48944

Reputation: 301

Pass Currently Clicked Button As Argument to on_press in Kivy (Python)

I am trying to remove the currently pressed button when it is clicked. My code is as follows:

self.tp.get_current_tab().content.children[0].remove_widget(self.btn)
self.tp.switch_to(self.tp.get_current_tab())

However self.btn gets overwritten. Here's where I am creating my buttons:

self.btn = Button(text=self.btn.text, size_hint=(0.5,0.5), on_press=self.remove_filter_btn)

Is there a way to pass the currently clicked button to remove_filter_btn?

Thanks.

Upvotes: 2

Views: 659

Answers (1)

FJSevilla
FJSevilla

Reputation: 4533

remove_filter_btn should already receive the instance of the calling button as argument.

Example:

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


class MainWindow(BoxLayout):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.orientation= 'vertical'
        for i in range(5):
            self.btn = Button(text='Button ' + str(i) ,
                              on_press=self.remove_filter_btn)
            self.add_widget(self.btn)

    def remove_filter_btn(self,  instance):
        self.remove_widget(instance)

class MyApp(App):
    def build(self):
        return MainWindow()

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

Execution:

enter image description here

Upvotes: 2

Related Questions