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

Kivy MD DropDownItem技术问题:报错、联动、宽度与禁用设置

Kivy MD开发常见问题解答

1. 封装到View类后出现「AttributeError: 'super' object has no attribute 'getattr'」的原因及解决

这个错误通常和以下场景相关:

  • 继承基类错误:如果View类继承自Widget而非Kivy MD的容器类(如MDBoxLayout、MDWidget),当访问self.ids或KV中引用的属性时,基类未实现__getattr__处理未找到的属性,就会触发该错误。
  • 错误重写__getattr__:若在View类中自定义了__getattr__方法,但未正确处理属性不存在的场景,调用super().__getattr__()时会因父类无该方法报错。
  • KV文件属性引用错误:KV中引用了View类未定义的属性,或控件ID拼写错误,会触发属性查找流程,最终导致super的__getattr__报错。

解决方法:

  • 确保View类继承自正确的Kivy MD基类,比如MDBoxLayout;
  • 不要随意重写__getattr__,如果必须重写,要添加属性存在性判断,避免直接调用super的该方法;
  • 检查KV文件中所有属性、控件ID是否与View类定义一致。

2. 实现两个DropDownItem双向联动

核心思路是绑定两个控件的文本变更事件,当其中一个选中选项时同步更新另一个的文本,同时避免循环触发。

示例代码:

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.dropdownitem import MDDropDownItem
from kivymd.uix.list import OneLineListItem

class View(MDBoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = "horizontal"
        
        # 初始化两个下拉项
        self.dd1 = MDDropDownItem(text="请选择")
        self.dd2 = MDDropDownItem(text="请选择")
        
        # 添加选项到下拉菜单
        options = ["选项A", "选项B", "选项C"]
        for opt in options:
            item = OneLineListItem(text=opt, on_release=self.on_option_select)
            self.dd1.dropdown.add_widget(item)
            self.dd2.dropdown.add_widget(item)
        
        # 绑定文本变更事件
        self.dd1.bind(on_text=self.sync_dropdown)
        self.dd2.bind(on_text=self.sync_dropdown)
        
        self.add_widget(self.dd1)
        self.add_widget(self.dd2)
    
    def on_option_select(self, item):
        # 设置当前点击的下拉项文本
        self.dd1.text = item.text
        self.dd2.text = item.text
    
    def sync_dropdown(self, instance, text):
        # 同步另一个下拉项的文本,避免循环触发
        target = self.dd2 if instance == self.dd1 else self.dd1
        if target.text != text:
            target.text = text

class MyApp(MDApp):
    def build(self):
        return View()

if __name__ == "__main__":
    MyApp().run()

3. 设置DropDownItem宽度为dp(80)

DropDownItem内部包含text_field子控件,默认size_hint_x=1会撑满父容器,因此需要同时设置DropDownItem本身和内部text_field的宽度:

方法1:Python代码中设置

from kivy.metrics import dp

# 初始化下拉项
dd_item = MDDropDownItem(text="测试")
# 关闭自动宽度,设置固定宽度
dd_item.size_hint_x = None
dd_item.width = dp(80)
# 同步设置内部文本框的宽度
dd_item.text_field.size_hint_x = None
dd_item.text_field.width = dp(80)

方法2:KV语言中设置

<View>:
    MDDropDownItem:
        size_hint_x: None
        width: dp(80)
        text_field:
            size_hint_x: None
            width: dp(80)

4. 启用/禁用DropDownItem

DropDownItem的active属性用于控制下拉菜单是否展开,禁用控件需要使用disabled属性:

# 禁用DropDownItem
self.dd_item.disabled = True
# 启用DropDownItem
self.dd_item.disabled = False

# 可选:修改禁用状态下的文本颜色
self.dd_item.disabled_color = (0.6, 0.6, 0.6, 1)

在KV语言中同样使用disabled属性:

<MDDropDownItem>:
    disabled: True  # 初始禁用
    disabled_color: 0.6, 0.6, 0.6, 1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:40:38