I have a test application which contains an Accordion. I want to be able to add items to the Accordion from within my python code.
Here is the relevant kv code:
<code><LayoutTest>:
BoxLayout:
id: mainBox
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 0.16, 0.11, 0, 1 # brownish color
Rectangle:
pos: self.pos
size: self.size
TabbedPanel:
id: tp
do_default_tab: False
TabbedPanelItem:
id: tab_1
text: 'Tab1R'
on_release: app.showTab1R()
ListView:
id: tab1_list
orientation: 'vertical'
<ListItem@AccordionItem>:
title: '2024/12/20 22:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, None
Label:
text: 'Content 3'
text_size: self.width, None
<ListView@BoxLayout>:
Accordion:
id: accordyN
orientation: 'vertical'
AccordionItem:
title: '2024/12/20 00:00 <some text>'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
# height: self.minimum_height
# height: type_label.height + content_label.height
valign: 'top'
Label:
id: type_label
text: 'Type 1'
text_size: self.size
halign: 'left'
valign: 'top'
Label:
id: content_label
text: 'Content 1'
text_size: self.size
halign: 'left'
valign: 'top'
AccordionItem:
title: '2024/12/20 09:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, self.height
Label:
text: 'Content 2'
text_size: self.width, self.height
</code>
<code><LayoutTest>:
BoxLayout:
id: mainBox
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 0.16, 0.11, 0, 1 # brownish color
Rectangle:
pos: self.pos
size: self.size
TabbedPanel:
id: tp
do_default_tab: False
TabbedPanelItem:
id: tab_1
text: 'Tab1R'
on_release: app.showTab1R()
ListView:
id: tab1_list
orientation: 'vertical'
<ListItem@AccordionItem>:
title: '2024/12/20 22:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, None
Label:
text: 'Content 3'
text_size: self.width, None
<ListView@BoxLayout>:
Accordion:
id: accordyN
orientation: 'vertical'
AccordionItem:
title: '2024/12/20 00:00 <some text>'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
# height: self.minimum_height
# height: type_label.height + content_label.height
valign: 'top'
Label:
id: type_label
text: 'Type 1'
text_size: self.size
halign: 'left'
valign: 'top'
Label:
id: content_label
text: 'Content 1'
text_size: self.size
halign: 'left'
valign: 'top'
AccordionItem:
title: '2024/12/20 09:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, self.height
Label:
text: 'Content 2'
text_size: self.width, self.height
</code>
<LayoutTest>:
BoxLayout:
id: mainBox
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 0.16, 0.11, 0, 1 # brownish color
Rectangle:
pos: self.pos
size: self.size
TabbedPanel:
id: tp
do_default_tab: False
TabbedPanelItem:
id: tab_1
text: 'Tab1R'
on_release: app.showTab1R()
ListView:
id: tab1_list
orientation: 'vertical'
<ListItem@AccordionItem>:
title: '2024/12/20 22:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, None
Label:
text: 'Content 3'
text_size: self.width, None
<ListView@BoxLayout>:
Accordion:
id: accordyN
orientation: 'vertical'
AccordionItem:
title: '2024/12/20 00:00 <some text>'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
# height: self.minimum_height
# height: type_label.height + content_label.height
valign: 'top'
Label:
id: type_label
text: 'Type 1'
text_size: self.size
halign: 'left'
valign: 'top'
Label:
id: content_label
text: 'Content 1'
text_size: self.size
halign: 'left'
valign: 'top'
AccordionItem:
title: '2024/12/20 09:00 <some text>'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, self.height
Label:
text: 'Content 2'
text_size: self.width, self.height
Using this layout, my application can display the Accordion, as well as the two sample AccordionItems shown above.
In my Python code, I somehow need to access the Accordion in order to add widgets to it. Here’s my code which doesn’t work:
<code>...
def showTab1R(self):
self.updateList(None)
def updateList(self):
entry = ListItem(title='2024/12/20 22:00 <some text>')
mainBox.accordyN.add_widget()
</code>
<code>...
def showTab1R(self):
self.updateList(None)
def updateList(self):
entry = ListItem(title='2024/12/20 22:00 <some text>')
mainBox.accordyN.add_widget()
</code>
...
def showTab1R(self):
self.updateList(None)
def updateList(self):
entry = ListItem(title='2024/12/20 22:00 <some text>')
mainBox.accordyN.add_widget()
In the Python code above, ListItem and mainBox are unrecognized (‘not defined’). How do I access these kv widgets from my Python code?