mnikley
mnikley

Reputation: 1645

Kivy popup in python with multiple on_release actions on button

Recently i changed my code because my GUI got too complex and i wanted to code my popups in python while my other GUI elements are created in a separate kivy file. From kivy i call the popup from a button and an on_release event:

Button:
    on_release:
        root.confirmPopup()

In python i have the following definition (dont mind the indentation):

    def confirmPopup(self): #call from kivy-file with root.confirmPopup()
        #create popup
        self.confPop = Popup()
        self.confPop.title = 'Confirm Action'
        self.confPop.auto_dismiss = False
        self.confPop.size_hint =  (None, None)
        self.confPop.size = (400, 300)
        #create popup-content
        # def confAct():
        #     lambda *args: self.confPop.dismiss()
        #     print('test')

        confBox = BoxLayout()
        confBox.orientation = 'vertical'
        confBox.add_widget(Label(text='Please confirm your action!',
                             pos_hint = {'center_x': .5, 'center_y': .5},
                             halign='center'))
        confBox.add_widget(Button(text='Accept'))
        confBox.add_widget(Button(text='Cancel',
                           on_release=lambda *args: self.confPop.dismiss()))
                           #on_release=confAct()))
        #add content, open popup
        self.confPop.content = confBox
        self.confPop.open()

As you can see i tried to create an inner function which i commented since it didnt work properly. My question is: how do i add multiple actions to on_release? I could add one action to on_press and one to on_release, but thats not what i want. I tried binding multiple on_release events to the button, separating commands with ; , and so on, but nothing worked. In kivy i could just add a new line with indentation for each command after on_release.

Upvotes: 1

Views: 1164

Answers (2)

mnikley
mnikley

Reputation: 1645

Figured it out, i needed an additional lambda *args: before my inner function call. Full code:

    def confirmPopup(self): #call from kivy-file with root.confirmPopup()
        #create popup
        self.confPop = Popup()
        self.confPop.title = 'Confirm Action'
        self.confPop.auto_dismiss = False
        self.confPop.size_hint =  (None, None)
        self.confPop.size = (250, 250)
        #create popup-content      
        confBox = BoxLayout()
        confBox.orientation = 'vertical'
        confBox.spacing = 5
        confBox.padding = (30,30,30,30)
        confBox.add_widget(Label(text='Please confirm your action!',
                              pos_hint = {'center_x': .5, 'center_y': .5},
                              halign='center'))
        confBox.add_widget(Button(text='Accept',
                                  size_hint=(1,0.5),
                                  on_release=lambda *args: confAcc()))
        confBox.add_widget(Button(text='Cancel',
                                  size_hint=(1, 0.5),
                                  on_release=lambda *args: self.confPop.dismiss()))
        #inner function
        def confAcc():
            self.confPop.dismiss()
            print('miau')
        #add content, open popup
        self.confPop.content = confBox
        self.confPop.open()

Might not be the best solution but it works. Call from kivy-file stays the same:

Button:
    on_release:
        root.confirmPopup()

Upvotes: 0

Lothric
Lothric

Reputation: 1318

You can't set multiple on_release (or on_press) functions. But why don't you just create function that calls other functions you need and call it by button?

Button(..., on_release=function)

def function():
    function2()
    function3()
    function4()
    ...

Upvotes: 3

Related Questions