Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The Button is the label with related actions that are triggered when the button is clicked. A kivy button is nothing but a fancy label whose action may vary according to the user's need and the kivy button gets triggered when a user clicks on it. We can use kivyMD with kivy in order to beautify the kivy buttons.
You can add a function behind the button and style the button, but to disable kivy button, we have a property:
disabled: true
This property will disable kivy button, which means the button will be present there but that will be of no use as it is disabled.
A basic approach to disable kivy button
The approach to disable kivy button is as follows:-
Import kivy
Import kivyApp
Import Widget
Import Button
Set minimum version
Create widget class: 1. Arrange a callback 2. Define the Callback function
In this example, we will disable the kivy button with the help of the .kv file. In this program, we have created a method called disable_other for activating and disabling the button and we have created a class called shot which will handle all the function calls in the program.
Main.py file
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_file('main.kv')
class Shot(Widget):
# Method to disable one button and activate the other.
def disable_other(self):
if self.ids.but_two.disabled == False:
self.ids.but_two.disabled = True
self.ids.but_one.disabled = False
print('Button One is now disabled. Button Two is now enabled.')
else:
self.ids.but_one.disabled = True
self.ids.but_two.disabled = False
print('Button One is now enabled. Button Two is now disabled.')
class ShotApp(App):
def build(self):
return Shot()
if __name__ == '__main__':
ShotApp().run()
You can also try this code with Online Python Compiler
In this code, we have disabled the kivy and shown it in the output by dividing the section for buttons.
Output:
In the above example, as we can see that we have created two files one that handles all the methods of the class i.e main.py file, and the other that handles the status of the button i.e main.kv file. And finally, in the output, it is shown which button is enabled which means if we click on Button1 it will be grey and the other button will be black.
Code 2
In this example, we will disable the kivy button with the help of the .kv file. In this program, we have created a method called disable_other for activating and disabling the button and we have created a class called shot which will handle all the function calls in the program.
Main.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_file('main.kv')
class Shot(Widget):
# Method to disable one button and activate the other.
def disable_other(self):
if self.ids.but_two.disabled == False:
self.ids.but_two.disabled = True
self.ids.but_one.disabled = False
# Add some color to make it more visual for which one is active.
self.ids.but_one.background_color = (0,0,1,1)
self.ids.but_one.color = (1,1,1,1)
self.ids.but_two.background_color = (0,0,0,1)
self.ids.but_two.color = (0,0,0,1)
print('Button One is now disabled. Button Two is now enabled.')
else:
self.ids.but_one.disabled = True
self.ids.but_two.disabled = False
# Add some color to make it more visual for which one is active.
self.ids.but_two.background_color = (0,0,1,1)
self.ids.but_two.color = (1,1,1,1)
self.ids.but_one.background_color = (0,0,0,1)
self.ids.but_one.color = (0,0,0,1)
print('Button One is now enabled. Button Two is now disabled.')
class ShotApp(App):
def build(self):
return Shot()
if __name__ == '__main__':
ShotApp().run()
You can also try this code with Online Python Compiler
In the above example, as we can see we have shown two outputs, in output 1 “Button one” is clicked, and in output 2 “Button two” is clicked. So with the help of functions used in main.py and in main.kv file by the "disabled=true " command, we were able to disable kivy button and generate the above output.
Frequently Asked Questions
How do we disable kivy button?
To disable the kivy button we use a Boolean statement from kivy, Disabled: True.
How to use the toggle button in kivy?
The Toggle kivy button acts like a checkbox, when you touch or click, the state toggles between ‘down’ and ‘normal’.
How to disable buttons in python?
You have to set the state option to disabled to grey out the button and make it insensitive.
What is KivyMD?
KivyMD is a collection of material design widgets to be used with kivy which is built on the top of the kivy library.
How do we remove widgets from Kivy?
In kivy, widgets are organized in trees. We can remove widgets using the command remove_widget() which is used to remove a widget from the children list and clear_widgets() which is used to remove all children from the widgets.
What is an instance in Kivy?
Instance in kivy is the name and reference to the object instance of the class customBnt when you press the button.
Conclusion
In this article, we have extensively discussed how to disable kivy button using the .kv file. We started with a brief introduction to kivy buttons, then how to disable kivy button with the help of examples.