Thorin Schiffer
Thorin Schiffer

Reputation: 2846

Kivy: access button image object

Kivy Button widget has background_normal and background_down properties for setting background image in normal and down state. Is there a way to access them as Image class instances to modify them? Or is there some API for direct manipulation of background's

and so on?

Upvotes: 0

Views: 820

Answers (1)

inclement
inclement

Reputation: 29450

No, you can't access them like that because they actually aren't Image widgets but implemented with graphics instructions. You can see that at https://github.com/kivy/kivy/blob/master/kivy/data/style.kv#L18 if you are interested.

If you want some different behaviour, I'd suggest making your own ImageButton kind of class. The current development version of Kivy (1.8, to be released soon) includes some changes to make this particularly easy by abstracting the button's behaviour as a ButtonBehavior class containing/managing the touch and on_press event etc. You can combine that with an Image by doing something like class ImageButton(ButtonBehavior, Image) to get an Image with touch interaction exactly like a button - though in this example you'd have to manage the background changing yourself if you wanted that. You can also make other different widgets via similar principles.

Making your own widget should make it easy to control the properties you sugest. Size and position are of course easy to manually manage once you build your button from component widgets that you can control. Repetition can be controlled by modifying the Texture of the image (see the 'wrap' property), though maybe you already know that. Opacity is a controllable property of every widget already, and of course aspect ratio is exposed as an Image property.

So overall, probably the best way to proceed is to make your own widget from an Image and add the touch interaction yourself. In Kivy 1.8 you can use the ButtonBehavior to make it easy. In 1.7 it might be easiest to place a transparent Button on top of an Image.

Upvotes: 2

Related Questions