KivyMD Dynamic Tab Management with different 'iterations' of tabs

Viewed 788

I've used the code from KivyMD documents about 'Dynamic Tab Management' so users can add / delete tabs. However, each of these tabs that are created are obviously identical and therefore the widgets that I put in it are too. This means that if I'm trying to take the id of said widget from e.g Tab 3, there isn't a way to do so as it is the same id as the widget in e.g Tab 1. Here is the code:

.py file

def on_start(self):
    self.add_tab()

def get_tab_list(self):
    print(self.root.ids.addworkouts.ids.tabs.get_tab_list())

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

def remove_tab(self):
    self.index -= 1
    self.root.ids.addworkouts.ids.tabs.remove_widget(
        self.root.ids.addworkouts.ids.tabs.get_tab_list()[0]
    )

.kv file

<AddWorkouts>
name: 'AddWorkouts'
tabs: tabs

BoxLayout:
    orientation: 'vertical'

    MDToolbar:
        title: ' '#app.getWorkoutName()
        type: 'top'
        left_action_items: [['keyboard-backspace', lambda x: app.goBacktoMyWorkouts()]]
        #md_bg_color: app.theme_cls.accent_color
        elevation: 10


    FloatLayout:
        canvas:
            Color:
                rgba: 0, 0, 0.5, 0.9
            Rectangle:
                pos: self.pos
                size: self.size

        MDTabs:
            id: tabs

        FloatLayout:
            canvas:
                Color:
                    rgba: 1, 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos

            pos_hint: {'center_x': 0.5, 'y': 0.1}
            size_hint: 0.8, 0.6


            MDTextField:
                pos_hint: {'x': 0.05, 'y': 0.8}
                size_hint: 0.6, None
                hint_text: 'Exercise Name'
                helper_text_mode: 'on_focus'
                required: 'True'
                multiline: False


<Tab>

    MDList:

        MDBoxLayout:
            adaptive_height: True
            md_bg_color: 1, 1, 1, 1

            MDFlatButton:
                text: "ADD EXERCISE"
                text_color: 16/255, 167/255, 249/255, 1
                on_release: app.add_tab()

            MDFlatButton:
                text: "REMOVE LAST EXERCISE"
                text_color: 16/255, 167/255, 249/255, 1
                on_release: app.remove_tab()

Tab 1, where the input in MDTextField is 'Hi':

enter image description here

Tab2, where the input in MDTextField has duplicated from Tab:

enter image description here Is there anyway of still having the users being able to add and delete Tabs, but make it so all the widgets (like the MDTextField in the .kv file) have different ids so I can access the user input from them? Thank you in advance, if this question has been worded badly please just ask for any more info!

1 Answers

The ids are only populated for widgets created in a kv rule. So any Tab created outside of the .kv file will not be entered into the ids dictionary. However, you can hack them into the ids by modifying your add_tab() method:

import weakref
def add_tab(self):
    self.index += 1
    new_tab = Tab(text=f"Exercise {self.index}")
    new_id = 'tab_' + str(self.index)
    self.root.ids.addworkouts.ids.tabs.add_widget(new_tab)
    self.root.ids.addworkouts.ids.tabs.ids[new_id] = weakref.ref(new_tab)

This adds the new_id to the ids in the MDTabs.

To make the MDTextField part of the Tab, just add it to the <Tab> rule. Perhaps like this:

<Tab>
    MDList:
        MDTextField:
            size_hint: 0.6, None
            hint_text: 'Exercise Name'
            helper_text_mode: 'on_focus'
            required: 'True'
            multiline: False

        MDBoxLayout:
            adaptive_height: True
            md_bg_color: 1, 1, 1, 1

            MDFlatButton:
                text: "ADD EXERCISE"
                text_color: 16/255, 167/255, 249/255, 1
                on_release: app.add_tab()

            MDFlatButton:
                text: "REMOVE LAST EXERCISE"
                text_color: 16/255, 167/255, 249/255, 1
                on_release: app.remove_tab()

Of course, you would remove the MDTextField (and its enclosing FloatLayout) from the <AddWorkouts> rule.

Related