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

Kivy中Popup内按钮跳转指定Screen失败问题求助

问题分析与解决

错误根源

  1. Screen嵌套不符合规则:在kv文件的<WindowManager>配置中,你把TensaoDeLinha和Corrente作为Calculadoras的子控件嵌套了,但Kivy的ScreenManager只能直接管理顶级Screen,嵌套在其他Screen内的Screen不会被注册到管理器中,因此触发ScreenManagerException提示找不到目标屏幕。
  2. Popup未自动关闭:跳转屏幕时,打开的Popup会残留界面,影响交互体验。

修复步骤

1. 调整ScreenManager的子项结构

修改kv文件中<WindowManager>部分,将TensaoDeLinha和Corrente直接作为WindowManager的顶级子控件,与其他Screen同级:

<WindowManager>:
    HomeScreen:
    Fabricantes:
    Calculadoras:
    TensaoDeLinha:
    Corrente:

2. 跳转时关闭Popup

修改Calculadoras中按钮的事件逻辑,在切换屏幕的同时关闭当前Popup:

<Calculadoras>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: "Tensão de Linha [V]"
            pos_hint: {"center_x": 0.5}
            on_press: 
                app.root.current = 'TensaoDeLinha'
                # 通过父控件层级找到Popup实例并关闭
                self.parent.parent.parent.dismiss()
        # 其余按钮代码保持不变

3. 确认Screen的name属性

确保所有需要切换的Screen都正确设置了name属性,你的TensaoDeLinha已经配置name: 'TensaoDeLinha',无需修改。

关键修改后的完整kv片段

#:kivy 1.0

<WindowManager>:
    HomeScreen:
    Fabricantes:
    Calculadoras:
    TensaoDeLinha:
    Corrente:

# 其余Screen规则保持不变...

<Calculadoras>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: "Tensão de Linha [V]"
            pos_hint: {"center_x": 0.5}
            on_press: 
                app.root.current = 'TensaoDeLinha'
                self.parent.parent.parent.dismiss()
        # 其余按钮代码...

内容的提问来源于stack exchange,提问作者Ruan Carlo Weiers Britzke

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 06:21:29