AttributeError: 'float' object has no attribute 'size'

498 views
Skip to first unread message

Gaurav Bhoi

unread,
May 19, 2021, 1:05:41 PM5/19/21
to Kivy users support
Hello guys i m getting this error please explain me why i m getting this error and help me solve it i m pesting codes and error snap down 

python code: 

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

class Box(BoxLayout):
def data(self):
#some operation
msg = 'test'
Box.Add_Data(self, msg)

def Add_Data(self, message):
lbl = Label(text=message, font_size=17, text_size=self.size,
halign='right', valign='middle', padding=(20, 0),
height=self.minimum_height - 10, size_hint_y=None)
self.ids.messages.add_widget(lbl)

class Test(App):
def build(self):
Clock.schedule_interval(Box.data, .1)
return Box()

if __name__ == '__main__':
Test().run()

Gaurav Bhoi

unread,
May 19, 2021, 1:10:56 PM5/19/21
to Kivy users support
i accidently posted this message half written. so i m writting this again

python :

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

class Box(BoxLayout):
    def data(self):
        #some operation
        msg = 'test'
        Box.Add_Data(self, msg)

    def Add_Data(self, message):
        lbl = Label(text=message, font_size=17, text_size=self.size,
                    halign='right', valign='middle', padding=(20, 0),
                    height=self.minimum_height - 10, size_hint_y=None)
        self.ids.messages.add_widget(lbl)

class Test(App):
    def build(self):
        Clock.schedule_interval(Box.data, .1)
        return Box()

if __name__ == '__main__':
    Test().run()

kv code:
<Box>:
    ScrollView:
        BoxLayout:
            id: messages
            orientation: 'vertical'
            size_hint_y: None
            height: self.minimum_height
            #padding: 20

error:

chat application v1 – test.py 19-05-2021 22_23_51.png

Elliot Garbus

unread,
May 19, 2021, 2:23:27 PM5/19/21
to kivy-...@googlegroups.com

I made the following changes to your python code as detailed in the comments.

There are some basics of OOP that I think you would benefit from, I like this article: https://realpython.com/python3-object-oriented-programming/

 

 

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label


class Box
(BoxLayout):
   
def data(self, dt):  # the callback for a Clock.schedule_once gets passed dt
        #some operation
       
msg = 'test'
       
self.add_data(msg)  # Box.add_message could only be used if add_data was a static method

   
def add_data(self, message):  # Python convention for method names, see PEP8
       
lbl = Label(text=message, font_size=17, text_size=self.size,
                   
halign='right', valign='middle', padding=(20, 0),
                   
height=25, size_hint_y=None# set the height of the label
       
self.ids.messages.add_widget(lbl)

class TestApp(App):
   
def build(self):
        b = Box()    # instance the class
        Clock.schedule_interval(b.data,
.1)  # use the instance to set the callback
       
return b

if __name__ == '__main__':
    TestApp().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/6be9eb42-dc78-4ffe-b6f4-d19fbae92150n%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages