Why StringProperty?

473 views
Skip to first unread message

coutinh...@gmail.com

unread,
Sep 8, 2021, 4:59:51 PM9/8/21
to Kivy users support
Hey guys.
I met python recently and I confess, I have a very slow learning process.

Everything I do is for fun, I'm not paid for programming but this has become my exhaust valve, similar to a mental exercise.

Until today I haven't understood the use of propertys, especially StringProperty, NumericProperty and others.

Following the documentation, I saw this example below:


In it there is a CustomOneLineIconListItem class that inherits from OneLineIconListItem and has a StringProperty()

My questions are:
1 - What are the Propertys?
2 - In the example above, what is the role of this StringProperty?

Despite being an example of KivyMD, I would be very grateful if it is read and answered with patience.

Elliot Garbus

unread,
Sep 8, 2021, 5:40:26 PM9/8/21
to kivy-...@googlegroups.com

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.

 

Elliot Garbus

unread,
Sep 8, 2021, 5:44:18 PM9/8/21
to kivy-...@googlegroups.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.

--

Degenerate Tech

unread,
Sep 10, 2021, 12:35:54 PM9/10/21
to Kivy users support
Reply all
Reply to author
Forward
0 new messages