You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中调用.kv文件定义的Kivy弹窗实现条件触发

问题描述

我编写了一个Python函数show_popup(result),该函数会先显示一个主弹窗;当用户点击任意位置关闭主弹窗后,需要根据传入的result参数值显示不同的弹窗。直接在Python中创建弹窗样式时,Canvas部分遇到诸多问题,但在.kv文件中定义的PopupTrue和PopupFalse样式显示正常。我希望主弹窗仍由Python代码触发,关闭后调用.kv文件中定义的对应弹窗。

现有代码

Python函数代码

def show_popup(result):
    def show_second_popup(self, *args):
        if result == True:
            # Show popup 1 
            pass
        elif result == False:
            # Show popup 2
            pass

    # Show the main popup
    popup_label = Label(markup= True, text_size=(190, 160), text= "This is the main popup" , 
                     halign="center", valign="center", color= (0,0,0,1),font_size= 24)
    popupWindow = Popup( 
                    auto_dismiss= True,
                    content= popup_label, 
                    size_hint= (0.8,0.4), 
                    pos_hint= {'x': 0.15,'y': 0.3}, 
                    title= "", separator_height= 0
                    )
    
    popupWindow.open()
    popupWindow.bind(on_dismiss= show_second_popup)

.kv文件弹窗样式代码

<PopupTrue@Popup>
    auto_dismiss: False
    title: ""
    separator_height: 0
    size_hint: 0.8, 0.6
    background_color: (0,0,0,0)
    background_normal: ''

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 0, 10, 10, 10
        border: 50
        border_color: (1,1,1,1)

        canvas.before:
            Color:
                rgba:(255/255,255/255,255/255,1)

            RoundedRectangle:
                pos: self.x - 20, self.y - 10
                size: self.width + 40, self.height - 60
                radius: [40]   

        Image:
            source: 'icon.png'

        Label: 
            text: "This popup appear if the condition was TRUE"
            color: 0,0,0,1
            font_size: 25
            
        CloseButton:
            text: "Close"
            color: 0,0,0,1
            size_hint: (None , None)
            width: 105
            height: 40
            pos_hint: {'center_x':0.5}
            on_release: root.dismiss() 

<PopupFalse@Popup>
    auto_dismiss: False
    title: ""
    separator_height: 0
    size_hint: 0.8, 0.6
    background_color: (0,0,0,0)
    background_normal: ''

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 0, 10, 10, 10
        border: 50
        border_color: (1,1,1,1)

        canvas.before:
            Color:
                rgba:(255/255,255/255,255/255,1)

            RoundedRectangle:
                pos: self.x - 20, self.y - 10
                size: self.width + 40, self.height - 60
                radius: [40]   

        Image:
            source: 'icon.png'

        Label: 
            text: "This popup appear if the condition was False"
            color: 0,0,0,1
            font_size: 25
            
        CloseButton:
            text: "Close"
            color: 0,0,0,1
            size_hint: (None , None)
            width: 105
            height: 40
            pos_hint: {'center_x':0.5}
            on_release: root.dismiss() 
解决方案

要调用kv文件中定义的自定义弹窗,通过Kivy的Factory类实例化即可,修改后的Python代码如下:

  1. 先导入Factory模块
from kivy.factory import Factory
  1. 调整show_popup函数逻辑
def show_popup(result):
    def show_second_popup(*args):
        if result:
            # 实例化并打开PopupTrue
            popup_true = Factory.PopupTrue()
            popup_true.open()
        else:
            # 实例化并打开PopupFalse
            popup_false = Factory.PopupFalse()
            popup_false.open()

    # 主弹窗创建逻辑保持不变
    popup_label = Label(markup=True, text_size=(190, 160), text="This is the main popup", 
                     halign="center", valign="center", color=(0,0,0,1), font_size=24)
    popupWindow = Popup( 
                    auto_dismiss=True,
                    content=popup_label, 
                    size_hint=(0.8, 0.4), 
                    pos_hint={'x': 0.15, 'y': 0.3}, 
                    title="", separator_height=0
                    )
    
    popupWindow.open()
    popupWindow.bind(on_dismiss=show_second_popup)

注意事项

  • 确保kv文件已通过Builder.load_file("你的文件名.kv")或App类指定的方式正确加载
  • 移除了show_second_popup函数中的self参数,因为它不属于类方法,无需该参数
  • 简化条件判断result == True为result,逻辑保持一致

内容的提问来源于stack exchange,提问作者MJ HH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 22:45:35