Here is some information on kivy properties.
https://kivy.org/doc/stable/gettingstarted/properties.html
https://kivy.org/doc/stable/api-kivy.properties.html?highlight=properties#module-kivy.properties
Kivy properties are actually Python Descriptors. Descriptors are a rather advanced Python concept. Read more here: https://docs.python.org/3/howto/descriptor.html
In the example you highlighted, copied below there are a few things going on.
class CustomOneLineIconListItem(OneLineIconListItem):
icon = StringProperty()
This creates the Kivy StringProperty called icon.
<CustomOneLineIconListItem>
IconLeftWidget:
icon: root.icon
The value of the String property icon in CustomOneLineIconListItem, is accessed here in kv as root.icon. If the kivy StringProperty icon changes, the value of IconLeftWidget.icon, will automatically update.
Even more confusing…. In a recycleview the attributes of the widget can be updated from the RecycleView.data list.
The viewclass is defined to be CustomOneLineIconListItem (see the code below), and where we see icon in the dict, refers to the icon StringProperty in CustomOneLineIconListItem.
def add_icon_item(name_icon):
self.ids.rv.data.append(
{"viewclass": "CustomOneLineIconListItem",
"icon": name_icon,
"text": name_icon,
"callback": lambda x: x,
} )
---- The KivyMD example you referred to ---
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen
from kivymd.icon_definitions import md_icons
from kivymd.app import MDApp
from kivymd.uix.list import OneLineIconListItem
Builder.load_string(
'''
#:import images_path kivymd.images_path
<CustomOneLineIconListItem>
IconLeftWidget:
icon: root.icon
<PreviousMDIcons>
MDBoxLayout:
orientation: 'vertical'
spacing: dp(10)
padding: dp(20)
MDBoxLayout:
adaptive_height: True
MDIconButton:
icon: 'magnify'
MDTextField:
id: search_field
hint_text: 'Search icon'
on_text: root.set_list_md_icons(self.text, True)
RecycleView:
id: rv
key_viewclass: 'viewclass'
key_size: 'height'
RecycleBoxLayout:
padding: dp(10)
default_size: None, dp(48)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
'''
)
class CustomOneLineIconListItem(OneLineIconListItem):
icon = StringProperty()
class PreviousMDIcons(Screen):
def set_list_md_icons(self, text="", search=False):
'''Builds a list of icons for the screen MDIcons.'''
def add_icon_item(name_icon):
self.ids.rv.data.append(
{"viewclass": "CustomOneLineIconListItem",
"icon": name_icon,
"text": name_icon,
"callback": lambda x: x,
} ) self.ids.rv.data = []
for name_icon in md_icons.keys():
if search:
if text in name_icon:
add_icon_item(name_icon)else:
add_icon_item(name_icon) class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = PreviousMDIcons()
def build(self):
return self.screen
def on_start(self):
self.screen.set_list_md_icons()
MainApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/17dcabc5-ad43-43b0-ad6a-1f13b5e96736n%40googlegroups.com.
Here is a simpler example using a kivy StringProperty to display the time.
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.label import Label
import time
kv = """
<TimeLabel>:
text: self.time_string
font_size: 40
BoxLayout:
TimeLabel:
"""
class TimeLabel(Label):
time_string = StringProperty(time.asctime()) # initialize, no delay showing time
def update_time(self, *args):
self.time_string = time.asctime()
def on_kv_post(self, base_widget): # execute after the kv code has been processed
Clock.schedule_interval(self.update_time, 1)
class TimeApp(App):
def build(self):
return Builder.load_string(kv)
if __name__ == "__main__":
TimeApp().run()
From: coutinh...@gmail.com
Sent: Wednesday, September 8, 2021 2:00 PM
To: Kivy users support
Subject: [kivy-users] Why StringProperty?
Hey guys.
--