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

2024-05-16 07:50:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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事件绑定到按钮,将命令与,诸如此类,但没有任何效果。在kivy中,我可以在on_发布后为每个命令添加一个带有缩进的新行


Tags: textselfaddreleaseonbuttoncontentwidget
2条回答

不能设置多个on_释放(或on_按下)功能。但为什么不创建一个调用其他需要的函数的函数,并通过按钮调用它呢

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

相关问题 更多 >