Kivy中Popup内按钮跳转指定Screen失败问题求助
问题分析与解决
错误根源
- Screen嵌套不符合规则:在kv文件的
<WindowManager>配置中,你把TensaoDeLinha和Corrente作为Calculadoras的子控件嵌套了,但Kivy的ScreenManager只能直接管理顶级Screen,嵌套在其他Screen内的Screen不会被注册到管理器中,因此触发ScreenManagerException提示找不到目标屏幕。 - 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
相关产品推荐
相关产品推荐

