Does a button have to be rectangle?

1,049 views
Skip to first unread message

nan

unread,
Jul 20, 2011, 4:31:36 AM7/20/11
to Kivy users support
Hi,

I am now sure if I used the Button class correctly. Actually I want a
round green button, so I made a green circle image (transparent
background) with Inkscape, and save it as round.png file.

Then I put the picture in my project folder and use:

button = Button(size_hint=(None, None), text='Click',
background_normal=''round.png', font_size= 20)

However, the button is not of round shape nor of rectangular shape, it
is distorted. How can I apply my texture to the button and make the
button of round shape? This is my first question.

My second questions is: how can I draw the button myself without using
a background image? I know by default it uses "‘data/images/
button.png’". Should I use button.canvas for drawing? could you give
me a simple example?

Mathieu Virbel

unread,
Jul 20, 2011, 5:57:47 AM7/20/11
to kivy-...@googlegroups.com
You must differenciate the Widget graphics and Widget bounding box.
The bounding box of the widget will be always expressed as a rectangle (pos + size)

So you can draw anything inside the bounding box (recommanded), like you can draw a circle, or even custom shape using image with transparency.

Then you have the collision with the widget: it's handled by the method collide_point(). You can overload it and implement a custom collisiion function using Vector.distance() for a circle, or even use Image.read_pixel (if Image(keep_data=True)) and check alpha part of the color.

And you'll have your result: drawing a custom shape button with adapted collision to the shape.

Mathieu

2011/7/20 nan <nahn...@googlemail.com>

nahn...@googlemail.com

unread,
Jul 20, 2011, 6:00:53 AM7/20/11
to kivy-...@googlegroups.com
Thanks Mathieu for your quick reply.

,Mathieu Virbel <m...@kivy.org> wrote:
> You must differenciate the Widget graphics and Widget bounding box.The bounding box of the widget will be always expressed as a rectangle (pos + size)
Reply all
Reply to author
Forward
0 new messages