'kivymd expansion panel - how to create panels with different content
Eorking with kivymd, and trying to create an expansion panel list with different content in each of the panels.
Tried to write an if statement inside the def on_start(self)
, like this:
def on_start(self):
for i in range(len(category_list)):
self.root.ids.box.add_widget(
MDExpansionPanel(
if i==0:
content=Content1()
else:
content=Content2(),
icon=f"{icon_list[i]}",
panel_cls=MDExpansionPanelTwoLine(
text=category_list[i].upper(),
secondary_text=category_secondary_list[i],
)
)
)
but didnt work. any idea how to tackle this?
my full code below:
from kivy.lang import Builder
from kivymd import images_path
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelTwoLine
KV = '''
<Content>
adaptive_height: True
BoxLayout:
spacing: "10dp"
padding:"10dp"
MDLabel:
text:"text"
Screen:
BoxLayout:
orientation: "vertical"
MDToolbar:
ScrollView:
GridLayout:
cols: 1
size_hint_y: None
height: self.minimum_height
id: box
'''
class Content(MDBoxLayout):
pass
category_list = ["pink", "purple", "yellow", "blue", "green","black", "white"]
category_secondary_list = ["pinkpinkpinkpinkpinkpinkpinkpinkpinkpinkpink",
"purplepurplepurplepurplepurple",
"yellowyellowyellowyellowyellow",
"blueblueblueblue",
"greengreengreengreen",
"blackblackblackblack",
"whitewhitewhitewhitewhite"]
icon_list = ["water-drop.png", "fast-food.png", "medetation.png", "cigarette.png", "band-aid.png", "mood.png",
"exercise.png"]
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
for i in range(len(category_list)):
self.root.ids.box.add_widget(
MDExpansionPanel(
content=Content(),
icon=f"{icon_list[i]}",
panel_cls=MDExpansionPanelTwoLine(
text=category_list[i].upper(),
secondary_text=category_secondary_list[i],
)
)
)
Test().run()
thank you!
Solution 1:[1]
Syntactically invalid Python:
MDExpansionPanel(
if i==0:
content=Content1()
else:
content=Content2(),
icon=f"{icon_list[i]}",
panel_cls=MDExpansionPanelTwoLine(
text=category_list[i].upper(),
secondary_text=category_secondary_list[i],
)
)
That's bad. Why? Because if ...:
and else:
statements are statements. They can't be embedded as expressions in a parameter list. Instead, you want...
Syntactically valid Python:
def on_start(self):
for i in range(len(category_list)):
self.root.ids.box.add_widget(
MDExpansionPanel(content=Content1())
if i==0 else
MDExpansionPanel(
content=Content2(),
icon=f"{icon_list[i]}",
panel_cls=MDExpansionPanelTwoLine(
text=category_list[i].upper(),
secondary_text=category_secondary_list[i],
)
)
)
That's good. Just invert your workflow by embedding the instantiation of MDExpansionPanel
objects inside an if ... else
ternary conditional expression rather than the other way around.
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 | marc_s |