I know that this question has probably already been asked many times, but I have a slightly different style structure. I am trying to write a dynamic 3D Card to a dynamic Tab by id, but an error comes out.
Here is a piece of KivyMD style:
<Tab>
ScrollView:
MDBoxLayout:
id: tab_box
orientation: "vertical"
adaptive_size: True
spacing: "56dp"
pos_hint: {"center_x": .5, "center_y": .5}
<MD3Card>
padding: 4
size_hint: None, None
size: "200dp", "100dp"
MDLabel:
id: card_label
text: root.text
adaptive_size: True
color: "grey"
pos: "12dp", "12dp"
bold: True
Here is a piece of code that should, by choosing a certain section in Tab, output certain data, for example, news in MD3Card as a list:
def on_tab_switch(
self, instance_tabs, instance_tab, instance_tab_label, tab_text
):
'''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <__main__.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
if tab_text == "section name":
newss = ["news1", "news2", "news3", "news4", "news5"]
for news in newss:
self.root.ids.tab_box.add_widget(
MD3Card(
text=news
)
)
This code cannot refer to the id that is inside the Tab.
An error comes out:
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'tab_box'
"AttributeError: 'super' object has no attribute '__getattr__'"
I would like to get a list with information inside Scroll when clicking on a section in Tab without using List and when clicking on any of the news, go to the information with this news already. I saw a solution with ObjectProperty(), but I didn't understand where to enter it, the same error was everywhere
Code snippet:
if tab_text == "section name":
newss = ["news1", "news2", "news3", "news4", "news5"]
for news in newss:
self.root.ids.tab_box.add_widget(
MD3Card(
text=news
)
)
Replace with:
if tab_text == "section name":
instance_tab.ids.tab_box.clear_widgets()
newss = ["news1", "news2", "news3", "news4", "news5"]
for news in newss:
instance_tab.ids.tab_box.add_widget(
MD3Card(
text=news
)
)