Introduction
Today, we will see how to create a button in Kivy and then disable buttons in kivy. Kivy is a platform-independent Graphical user interface tool in python that can run on android, ios, Linux, windows etc. Kivy is not only used for building the android application but also helps in the development of the desktop application.
Certain times we need to disable buttons in Kivy. So in this article, we will look at how to disable buttons and how to create a fully functional button in kivy.
Before going ahead, let us first see what a button is?
Button: Button in Kivy is a Label. On clicking the button, the associated actions are triggered. We can also add functions behind the button and can style it as well.
Now to disable buttons, there is a property of the button, i.e., disabled, that value must be set to True. Disabled property of the button helps in disabling the button. The button would be there, but none of its functionality would work after disabling it.
Create Button in Kivy
To work with the button first, we must import it. Import the button using the following commands.
from kivy.uix.button import Button
Code:-
# Base Class of App need to inherit from the APP class
from kivy.app import App
# Import Button to create a button in kivy. It gives an error if not
# imported
from kivy.uix.button import Button
# Box Layout helps us arrange widgets in a particular fashion
# that vertical fashion or horizontal fashion
from kivy.uix.boxlayout import BoxLayout
# Import config helps us change the default setting of kivy
from kivy.config import Config
Config.set('graphics', 'resizable', True)
# Creating App class CodingNinja
class CodingNinja(App):
# define build function
def build(self):
# Define the button and add button properties to it like font_size,background color,color,size etc of the button
btn = Button(text="This is a Button",
font_size="60sp",
background_color=(.67, 1, .33, 1),
color=(1, 1, 1, 1),
size=(100, 100),
size_hint=(.6, .6),
pos=(300, 250))
return btn
# Define main
if __name__=="__main__":
# Run app
CodingNinja().run()
Output:-