Rounding button corners in kivy

前端 未结 2 1144
花落未央
花落未央 2020-12-25 13:44

What is the preferred way to create rounded corners for buttons in kivy?

Are there other equally viable ways to perform this task? Thank you.

2条回答
  •  礼貌的吻别
    2020-12-25 14:36

    This is a tricky one. As far as I am concern Widgets are always rectangles. But we can change the background and put a couple of images for the normal and down states using the background_normal and background_down properties respectively. Also you will need to understand the border property.

    With this two images called normal.png and down.png, you can start adding your round borders.

    enter image description here enter image description here

    Here is the piece of code, which is very straight forward (I try to explain the border property below):

    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.button import Button
    from kivy.lang import Builder
    
    Builder.load_string("""
    :
        Button:
            background_normal: 'normal.png'
            background_down: 'down.png'
            border: 30,30,30,30
    """)
    
    
    class Base(FloatLayout):
        pass
    
    class ButtonsApp(App):
        def build(self):
            return Base()
    
    if __name__ == "__main__":
        ButtonsApp().run()
    

    The way I understand this (and I might be wrong) is this. The values in border: 30,30,30,30 tells how many pixels on the top, right, bottom and left are going to be used for the border of the background's button. The rest is going to be filled with the middle part. I am not sure here. By the way, If you want to see something cool, see for example border: 150,150,150,150. The reason is that we are picking up a border bigger than the actual image.

    The caveat: Widgets are still rectangles. That means that even if you click on the rounded corners, the button still receive the event. I guess it is a fair price. If you want to do something better, maybe I can help you but we will need to use some maths to collide the points. One of the tricks with the Pong Game tutorial in the documentation is that actually the ball is a square. I posted a related question here, but you will need to use the Canvas

提交回复
热议问题