Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing conditional arguments to kivy from python

The idea is pretty simple: I have a slider which affects the speed of a gif. However, I can't figure out how to trigger the image to get the updated "anim_delay" value from python (or for python to push it to kivy?).

The main file:

class BottomRightPart(BoxLayout):

    imgdelay=-1

    def OnFanSpeedSliderValueChange(self,slider,value):
        if value == 0:
            imgdelay = -1
        elif value == 1:
            imgdelay= 1
        elif value == 2:
            imgdelay = 0.6
        elif value == 3:
            imgdelay = 0.3
        elif value == 4:
            imgdelay = 0

The .kv code

<BottomRightPart>
    BoxLayout:  
        Image:
            id:FanImg
            source: 'fan.gif'
            anim_delay: root.imgdelay

        Label:
            text: str(int(FanSpeed.value))

        Slider:
            id:FanSpeed
            min:0
            max:4
            step:1
            on_value: root.OnFanSpeedSliderValueChange(*args)

Where's the missing link? Any pointers is appreciated!

like image 967
GroanMan Avatar asked Apr 23 '26 18:04

GroanMan


1 Answers

You are missing self. Also set imgdelay to a NumericProperty.

Try this:

from kivy.properties import NumericProperty


class BottomRightPart(BoxLayout):

    imgdelay = NumericProperty(-1)

    def OnFanSpeedSliderValueChange(self,slider,value):
        if value == 0:
            self.imgdelay = -1
        elif value == 1:
            self.imgdelay= 1
        elif value == 2:
            self.imgdelay = 0.6
        elif value == 3:
            self.imgdelay = 0.3
        elif value == 4:
            self.imgdelay = 0

Personally I would create my method something like this:

def OnFanSpeedSliderValueChange(self,slider,value):
    values = [-1, 1, 0.6, 0.3, 0]
    self.imgdelay = values[int(value)]
like image 76
el3ien Avatar answered Apr 28 '26 19:04

el3ien