Search code examples
python-3.xkivykivy-language

kivy object property is showing float object has no attribute


i am trying to make an animation with clock in kivy,when i execute the function with the help of button it works good but when i schedule the function in clock it throws an error.

my python code is

from kivy.app import App
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty
login=Builder.load_file('login.kv')
class Login(BoxLayout):
    image=ObjectProperty(None)
    def press(self):
        anim=Animation(size_hint=(.3,.3))
        anim &=Animation(pos_hint={'center_x':.5,'center_y':.8})
        anim.start(self.image)
    Clock.schedule_once(press)
class Main(App):
    def build(self):
        return Login()
if __name__=="__main__":
    Main().run()

my kivy file is:

<Login>:
image:img
Image:
    id:img
    source:'22.png' 
    pos_hint:{'center_x':.5,'center_y':.5}

while i execute the program it throws the following error:

**anim.start(self.image)

AttributeError: 'float' object has no attribute 'image'**


Solution

  • The problem is that your code Clock.schedule_once(press) is outside any method in the Login class, so it gets executed when the class is loaded, before the image property is assigned. So, the solution is to insure that press() is not called until the Login class is ready. You can do this by moving the Clock.schedule_once() call into the build() method of the App.

    class Login(BoxLayout):
        image=ObjectProperty(None)
    
        def press(self, dt):
            anim=Animation(size_hint=(.3,.3))
            anim &=Animation(pos_hint={'center_x':.5,'center_y':.8})
            anim.start(self.image)
    
    
    class Main(App):
        def build(self):
            login = Login()
            Clock.schedule_once(login.press)
            return login