如何彻底移除Kivy Popup的遮罩层?
解决Kivy Popup遮罩层拦截外部点击的问题
要彻底移除遮罩层并允许点击Popup外的交互区域,只需修改两个关键属性:
- 关闭模态拦截:添加
modal: False,这个属性默认是True,会强制拦截Popup之外的所有输入事件; - 隐藏遮罩视觉效果:把
overlay_color改成0, 0, 0, 0(全透明),彻底消除遮罩的显示。
修改后的AlertPopup代码如下:
<AlertPopup@Popup>: auto_dismiss: False title: "Notice" background: '' size_hint: (None,None) pos_hint: {'top': 0.9, 'right': 1.0} modal: False # 关闭模态模式,允许点击外部区域 overlay_color: 0, 0, 0, 0 # 设置为全透明,隐藏遮罩 background_color: 0, 0, 0, 0 canvas.before: Color: rgba: 243/255, 253/255, 18/255, 0.48 # Kivy颜色通道范围是0-1,需将0-255数值转换 Rectangle: pos: self.pos size: self.size BoxLayout: orientation: 'vertical' BoxLayout: orientation: 'horizontal' Button: size_hint: .15, 1.0 text: 'X' on_release: root.dismiss() Label: text: 'Some message here'
额外提个细节:你原代码中Color的rgba值用了243、253这类0-255的数值,Kivy的颜色通道实际是0-1的范围,所以要除以255才能显示正确的颜色,上面代码已修正这个问题。
内容的提问来源于stack exchange,提问作者Aviel Fedida
相关产品推荐
相关产品推荐

