У меня есть тестовое приложение, содержащее Accordion. Я хочу иметь возможность добавлять элементы в Accordion из моего кода Python.
Вот код kv:
Код: Выделить всё
#:kivy 1.0.9
:
BoxLayout:
id: mainBox
orientation: 'horizontal'
padding: 4
spacing: 4
canvas:
Color:
rgba: 0, 0.2, 0.4, 1 # Blue-ish color
Rectangle:
pos: self.pos
size: self.size
# workBox
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 0.16, 0.11, 0, 1 # brownish color
Rectangle:
pos: self.pos
size: self.size
ListView:
id: r_list
orientation: 'vertical'
[AccordionItemTitle@Label]:
text: ' ' + ctx.title
halign: 'left'
valign: 'center'
text_size: self.size
normal_background: ctx.item.background_normal if ctx.item.collapse else ctx.item.background_selected
disabled_background: ctx.item.background_disabled_normal if ctx.item.collapse else ctx.item.background_disabled_selected
canvas.before:
Color:
rgba: self.disabled_color if self.disabled else self.color
BorderImage:
source: self.disabled_background if self.disabled else self.normal_background
pos: self.pos
size: self.size
PushMatrix
Translate:
xy: self.center_x, self.center_y
Rotate:
angle: 90 if ctx.item.orientation == 'horizontal' else 0
axis: 0, 0, 1
Translate:
xy: -self.center_x, -self.center_y
canvas.after:
PopMatrix
:
title: '2024/12/20 22:00 '
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, None
Label:
text: 'Content 3'
text_size: self.width, None
:
Accordion:
id: accordyN
orientation: 'vertical'
AccordionItem:
title: '2024/12/20 00:00 '
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 '
BoxLayout:
orientation: 'vertical'
Label:
text: 'Type 1'
text_size: self.width, self.height
Label:
text: 'Content 2'
text_size: self.width, self.height
В моем коде Python я каким-то образом необходимо получить доступ к Аккордеону, чтобы добавить к нему виджеты. Вот мой код, который не работает:
Код: Выделить всё
import kivy
from kivy.app import App
# below this kivy version you cannot use the app
kivy.require('1.9.0')
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.accordion import AccordionItem
class LayoutTest(BoxLayout):
temp = 0
class ListItem(AccordionItem):
temp = 0
class LayoutTestApp(App):
mainLayout = None
def __init__(self, **kwargs):
super().__init__()
def build(self):
self.mainLayout = LayoutTest()
entry = ListItem(title='2024/12/20 22:00 ')
self.mainLayout.ids['accordyN'].add_widget(entry)
return self.mainLayout
if __name__ == '__main__':
LayoutTestApp().run()
self.mainLayout.ids['accordyN '].add_widget(entry)
'~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: ' AccordyN'
Как мне добавить больше виджетов KV в макет из моего кода Python?
Подробнее здесь: https://stackoverflow.com/questions/788 ... -accordion