IntegerProperty

72 views
Skip to first unread message

Andrew McLeod

unread,
Jul 17, 2019, 1:25:54 PM7/17/19
to Kivy users support
Is there a reason that Integer properties aren't implemented?

I'm currently fighting with some buttons which should automatically display text based on an integer but with a format string

e.g.

MyWidget:
    my_integer: 3  # actually a Python property with allownone=True and might be set to None
    text: 'my_integer: {}'.format(self.my_integer or '')

and then binding that my_integer value from another property somewhere else. If I use a StringProperty I have to cast things on the Python side, and then '0' evaluates as True which I don't want.
If I use a NumericProperty, I have to cast it to an int in the format string which wouldn't be too much of a hassle except for when it is None...

So given that integers are fairly common in Kivy, I was just wondering why there isn't an IntegerProperty?

Andrew

Alexander Taylor

unread,
Jul 17, 2019, 1:55:31 PM7/17/19
to Kivy users support
I'm not clear, what is your issue with storing integers in a NumericProperty? I just tried a simple test application and there was no implicit type conversion or other issue.

Enter code here...from kivy.properties import NumericProperty
from kivy.app import App
from kivy.lang import Builder

class TestApp(App):                                                                                                      
    number
= NumericProperty(0)                                                                                          
                                                                                                                         
   
def build(self):                                                                                                    
        root
= Builder.load_string('''                                                                                  
BoxLayout:                                                                                                              
    orientation: '
vertical'                                                                                              
    Label:                                                                                                              
        text: '
{} has type {}'.format(app.number, type(app.number))                                                      
    Button:                                                                                                              
        on_release: app.number += 1                                                                                      
        text: '
press me'                                                                                                
        '''
)                                                                                                            
       
return root                                                                                                      

TestApp().run()



Elliot Garbus

unread,
Jul 17, 2019, 2:09:34 PM7/17/19
to kivy-...@googlegroups.com

For what is worth, my kivy code would look like:

 

text: ‘my_integer: ‘ + str(my_integer) if my_integer else ‘’

--
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 post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/adc4b89d-6606-4c57-8eaa-1d035449fd9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Andrew McLeod

unread,
Jul 18, 2019, 1:48:09 AM7/18/19
to Kivy users support
I'd got a string output of 0.0 in my app, and assumed NumericProperty stored a float. I hadn't realised that the type of object passed would be stored within the NumericProperty with the value. So in this case probably user error...

Thanks anyway.
Reply all
Reply to author
Forward
0 new messages