python 中的 Kivy 弹出窗口,按钮上有多个 on_release 操作

Kivy popup in python with multiple on_release actions on button

最近我更改了我的代码,因为我的 GUI 变得太复杂了,我想在 python 中编写我的弹出窗口,而我的其他 GUI 元素是在一个单独的 kivy 文件中创建的。 从 kivy 我通过一个按钮和一个 on_release 事件调用弹出窗口:

Button:
    on_release:
        root.confirmPopup()

在python中我有以下定义(不要介意缩进):

    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()

如您所见,我尝试创建一个内部函数,我对此进行了评论,因为它无法正常工作。 我的问题是:如何向 on_release 添加多个操作? 我可以向 on_press 添加一个操作,向 on_release 添加一个操作,但那不是我想要的是。我尝试将多个 on_release 事件绑定到按钮,用 ; 分隔命令, 等等,但没有任何效果。在 kivy 中,我可以在 on_release.

之后为每个命令添加一个带有缩进的新行

您不能设置多个 on_release(或 on_press)函数。但是你为什么不创建一个函数来调用你需要的其他函数并通过按钮调用它呢?

Button(..., on_release=function)

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

我明白了,在我的内部函数调用之前我需要一个额外的 lambda *args:。完整代码:

    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()

可能不是最好的解决方案,但它确实有效。来自 kivy 文件的调用保持不变:

Button:
    on_release:
        root.confirmPopup()