'If I try to clear dynamically added tabs using clear_widgets function I get an error(mdtabs can remove only subclass of MDTabslabel or MDTabsBase

from kivy.lang import Builder from kivy.uix.scrollview import ScrollView

from kivymd.app import MDApp from kivymd.uix.tab import MDTabsBasefrom kivy.lang import Builder from kivy.uix.scrollview import ScrollView

from kivymd.app import MDApp from kivymd.uix.tab import MDTabsBase

KV = MDBoxLayout: orientation: "vertical"

MDToolbar:
    title: "Example Tabs"

MDTabs:
    id: tabs
MDList:

    MDBoxLayout:
        adaptive_height: True

        MDFlatButton:
            text: "ADD TAB"
            on_release: app.add_tab()

        MDFlatButton:
            text: "REMOVE LAST TAB"
            on_release: app.remove_tab()

        MDFlatButton:
            text: "GET TAB LIST"
            on_release: app.get_tab_list()

class Tab(ScrollView, MDTabsBase): Pass

class Example(MDApp): index = 0

def build(self):
    return Builder.load_string(KV)

def on_start(self):
    self.add_tab()

def get_tab_list(self):
    pass

    print(self.root.ids.tabs.get_tab_list())

def add_tab(self):
    self.index += 1
    self.root.ids.tabs.add_widget(Tab(text=f"{self.index} tab"))

def remove_tab(self):
    if self.index > 1:
        self.index -= 1
    self.root.ids.tabs.clear_widgets()

Example().run()



Solution 1:[1]

In my opinion, that is a bug in the MDTabs code. The MDTabs widget has a structure that includes an MDTabBar as a child of the MDTabs. The clear_widgets() code tries to remove that MDTabBar, but the MDTabs code will always raise an Exception when you try to remove that MDTabBar. When you use add_widget() on the MDTabs, the newly added widget gets added to a MDGridLayout inside that MDTabBar, so you can use clear_widgets() on that MDGridLayout instead of on the entire MDTabs. Here is a modified version of your remove_tab() method that uses that approach:

def remove_tab(self):
    if self.index > 1:
        self.index -= 1
    self.root.ids.tabs.ids.layout.clear_widgets()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Anderson