对象“<__main__.Tab >object at”的“被访问,它将在未来的版本中删除,python,kivy,kivymd"/>

首页 > 解决方案 > 警告:不推荐使用的属性“对象“<__main__.Tab >object at”的“被访问,它将在未来的版本中删除

问题描述

我正在尝试将图形添加到 KivyMD 的选项卡中,这是出现的警告,并且图形未显示在界面上。这是我从代码中收到的完整错误:

[警告] 对象“< main .Tab object at 0x0000011807CA3040>”的已弃用属性“”已被访问,将在未来版本中删除

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

plt.plot(x, y)
y_label = 'performance'


class Tab(FloatLayout, MDTabsBase):
     graph = StringProperty()


class StatsScreen(Screen):
    def __init__(self, **kwargs):
        super(StatsScreen, self).__init__()
        self.graph = self.ids.Last_graph
        self.graph.add_widget(FigureCanvasKivyAgg(plt.gcf()))

这是屏幕的 kivymd 代码:

<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'
        Tab:
            name: 'Last Session'
            text: 'Last Session'
            FloatLayout:
                id: Last_graph
        Tab:
            name: 'Progress Graph'
            text: 'Progress Graph'

    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>:
    text: 'testing'
    FloatLayout:
    id: test_id

有没有办法解决这个错误?

标签: pythonkivykivymd

解决方案


首先,它不是Error,而是Warning,如果您阅读消息的内容,实际上很清楚您正在访问Tab分类为 的类的属性,deprecated然后很快将被完全删除,尽管在此情况有点奇怪,因为显然它正在尝试访问一个名为"". 不用担心,当 Kivy 的未来版本发布时,警告文本应该会消失,但是在您等待时,您可以通过添加以下代码行来隐藏这些警告消息:

from kivy.config import Config
Config.set("kivy", "log_level", "error")
Config.write()

推荐阅读