【问题标题】:Kivy popup in python with multiple on_release actions on buttonpython中的Kivy弹出窗口,按钮上有多个on_release操作
【发布时间】:2020-06-16 18:11:56
【问题描述】:

最近我更改了我的代码,因为我的 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 之后为每个命令添加一个带有缩进的新行。

【问题讨论】:

    标签: python popup kivy


    【解决方案1】:

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

    Button(..., on_release=function)
    
    def function():
        function2()
        function3()
        function4()
        ...
    

    【讨论】:

    • 感谢对多个 on_release 绑定的澄清 :) 想通了
    【解决方案2】:

    想通了,在我的内部函数调用之前我需要一个额外的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-file 的调用保持不变:

    Button:
        on_release:
            root.confirmPopup()
    

    【讨论】:

      猜你喜欢
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多