Table of contents
1.
Introduction
2.
Properties
3.
Example
4.
FAQs
4.1.
What is a canvas in kivy?
4.2.
What is the float layout in kivy?
4.3.
What are Layouts?
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Shaping The Button

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a GUI tool that is platform-independent in Python. Kivy can be run on android, IOS, Linux, windows, etc. It is used to create the Android application, but this does not mean that we cannot use it on desktop applications. 

If we talk about widgets that are generally rectangles, we can change their background and put some images for the normal and the downstate of the button with some properties such as background_normal and background_down, respectively. Accordingly, for rounding or shaping the corners of the button, you must know one more property of a button which is the border property.

Properties

  • Background_down: It is a string property. When a button is pressed, the button's background image is used for the default graphical representation.

            Syntax: background_normal: ‘normal.png’

  • Background_normal: It is also a string property. But in this, When a button is not pressed, then the button's background image is used for the default graphical representation.

           Syntax: backgroubd_down: ‘down.png’

  • Border: It is used for border-image graphics instructions. It is used with background_down and background_normal. It can also be used for custom backgrounds. It is a List property, and the default is (16,16,16,16).

Syntax: border: 30,30,30,30 . This values in the border represents how many pixels are on the left,right,top and bottom.

Example

Code 1:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *

root=Widget()

class RoundCorner(RelativeLayout):
    def __init__(self,r=50,**kwargs):
        super(RoundCorner, self).__init__(**kwargs)
        self.surf=FloatLayout(); self.add_widget(self.surf)
        with self.canvas:
            Color(.3,0,3,0.3)
            Rectangle(pos=[-r,0],size=[r,self.size[1]])
            Rectangle(pos=[0,-r],size=[self.size[0],self.size[1]+2*r])
            Rectangle(pos=[self.size[0],0],size=[r,self.size[1]])


            Color(0,.3,0,.5)
            Ellipse(pos=[-r,-r],size=[2*r,2*r])
            Ellipse(pos=[self.size[0]-r,-r],size=[2*r,2*r])
            Ellipse(pos=[-r,self.size[1]-r],size=[2*r,2*r])
            Ellipse(pos=[self.size[0]-r,self.size[1]-r],size=[2*r,2*r])

             Color(1,1,1,0.3)
            self.bg=Rectangle(size=self.size)

root.add_widget(RoundCorner(size=[300,400],pos=[320,100]))
root.add_widget(RoundCorner(size=[100,100],pos=[100,200]))

class MyApp(App):
    def __init__(self):
        super(MyApp, self).__init__()
    def build(self):
        return root

if __name__=="__main__":
    MyApp().run()

Output:


                           Source: Stackoverflow

Code 2: In this, we are using two images, button1.png and button2.png:

import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.button import Button
kivy.require('1.9.0')
from kivy.config import Config

Config.set('graphics', 'resizable', True)

class ButtonApp(App):

def build(self):
btn = Button(text ="Click me !",
                    background_normal = 'button1.png',
                    background_down = 'button2.png',
                    border = (30, 30, 30, 30),                   
                    size_hint = (.3, .3),
                    pos_hint = {"x":0.35, "y":0.3}
                )
return btn
root = ButtonApp()
root.run()

Output:


 

FAQs

What is a canvas in kivy?

Ans: The Canvas is the primary subject for drawing by a widget. 

What is the float layout in kivy?

Ans: FloatLayout performs the pos_hint and size_hint properties of its children.

What are Layouts?

Ans: Layouts are containers used to organize widgets in a particular manner.

Key Takeaways

In this article, we discussed some of the properties used to shape the button using kivy in Python. If this article was helpful, share it with your friends.

To learn more on such topics, visit our website code studio

For learning on more topics you can jump to our links, bigdatadatabasesnon-relational-databases,  data-miningTop 100 sql-programs,   interview-experiences

 

Live masterclass