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

KivyMD向Tab添加图表时出现废弃属性警告且图表不显示如何解决

问题修复方案

1 警告修复(Deprecated property "text")

这个警告是因为你使用的新版KivyMD中,MDTabsBase组件的text属性已被废弃,替换为title属性,同时MDTabs的tab_display_mode取值规则也有更新,修改如下:

  • 将KV代码中MDTabs的tab_display_mode: 'text'修改为tab_display_mode: 'text-only'
  • 所有Tab节点的text属性统一替换为title属性,包括全局Tab规则和单个Tab实例的配置
  • 自定义Tab类中无用的graph属性可直接删除

2 图表不显示修复

图表无法加载是三个原因导致的:

  1. 你在StatsScreen的__init__方法中直接访问ids,此时Kivy还未完成布局加载,ids字典未初始化,找不到Last_graph节点
  2. StatsScreen的__init__方法没有将**kwargs传递给父类,导致控件初始化异常
  3. KV代码中<Tab>规则的缩进错误,id绑定位置不对

修改后的Python代码

from kivy.clock import Clock
# 其余原有导入保持不变

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [1, 2, 7, 4, 1, 6, 5, 8]

plt.figure() # 每次新建画布避免复用旧图
plt.plot(x, y)
plt.ylabel('performance')


class Tab(FloatLayout, MDTabsBase):
    # 移除无用的StringProperty
    pass


class StatsScreen(Screen):
    def __init__(self, **kwargs):
        super(StatsScreen, self).__init__(**kwargs) # 传递kwargs给父类

    def on_enter(self, *args):
        # on_enter在Screen进入时触发,此时ids已完成加载
        self.graph = self.ids.Last_graph
        self.graph.add_widget(FigureCanvasKivyAgg(plt.gcf()))

修改后的KV代码

<StatsScreen>
    name: 'stats_interface'
    MDFloatLayout:
        size: root.width, root.height
        md_bg_color: app.theme_cls.primary_light

        MDLabel:
            id: heading_label
            text: 'Statistics'
            size_hint: (0.75, 0.1)
            pos_hint: {'center_x':0.5 , 'y':0.90}
            bold: True
            halign: 'center'

        MDTabs:
            size_hint: 0.95, 0.53
            pos_hint: {'center_x':0.5 , 'y':0.4 }
            tab_display_mode: 'text-only' # 替换原有text取值
            Tab:
                name: 'Last Session'
                title: 'Last Session' # text替换为title
                FloatLayout:
                    id: Last_graph
            Tab:
                name: 'Progress Graph'
                title: 'Progress Graph' # text替换为title

        MDBoxLayout:
            orientation: 'vertical'
            size_hint: 0.95, 0.1
            pos_hint: {'center_x':0.5, 'top':0.39}
            md_bg_color: 250/255, 250/255, 250/255, 1
            line_color: 0, 0, 0, 1
            radius: [15, ]

            MDLabel:
                id: goal_grade_label
                text: 'Goal Grade: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.8}
                bold: True

            MDLabel:
                id: current_grade_label
                text: 'Current Grade: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.5}
                bold: True

        MDBoxLayout:
            orientation: 'vertical'
            size_hint: 0.95, 0.2
            pos_hint: {'center_x':0.5, 'top':0.28}
            md_bg_color: 250/255, 250/255, 250/255, 1
            line_color: 0, 0, 0, 1
            radius: [18, ]

            MDLabel:
                text: 'Last session analysis: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.8}
                bold: True

            MDLabel:
                id: percentage_label
                text: 'Percentage: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.6}

            MDLabel:
                id: grade_label
                text: 'Grade: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.4}

            MDLabel:
                id: marks_label
                text: 'Total Marks: '
                size: self.texture_size
                pos_hint: {'x':0.05 , 'center_y':0.2}

        MDTextButton:
            text: 'Return'
            size_hint: (0.2, 0.05)
            pos_hint: {'x': 0.1, 'y': 0.02}
            bold: True

        MDTextButton:
            text: 'Settings'
            size_hint: (0.2, 0.05)
            pos_hint: {'x': 0.475, 'y': 0.02}
            bold: True

        MDTextButton:
            text: 'Topics'
            size_hint: (0.2, 0.05)
            pos_hint: {'x': 0.85, 'y': 0.02}
            bold: True

<Tab>:
    title: 'testing' # text替换为title
    FloatLayout:
        id: test_id # 修正缩进,id属于FloatLayout

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 13:18:01