Table of contents
1.
Introduction
2.
A basic approach to disable kivy button
3.
Example:
4.
Frequently Asked Questions
4.1.
How do we disable kivy button?
4.2.
How to use the toggle button in kivy?
4.3.
How to disable buttons in python?
4.4.
What is KivyMD?
4.5.
How do we remove widgets from Kivy?
4.6.
What is an instance in Kivy?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Disable Kivy button using .kv file

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

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
  • Create App class
  • Create .kv file
    1. Create widget
    2. Create button
    3. Specify requirements
    4. Disable kivy button true if required
  • Return layout/widget/class
  • Run an instance of a class

Example:

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
Run Code

 

Main.kv file:

This is the main.kv file, in this code we have created two buttons "Button 1" and "Button 2" to show the status of the button.

<Shot>
BoxLayout:
orientation: "horizontal"
        size: root.width, root.height

        Button:
            id: but_one
            text: "Button 1"
            size_hint: (1,1)
            disabled: False
            on_press: root.disable_other()
            
        Button:
            id: but_two
            text: "Button 2"
            size_hint: (1,1)
            disabled: True
            on_press: root.disable_other()
You can also try this code with Online Python Compiler
Run Code

 

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
Run Code

 

Main.kv

This is the main.kv file, in this code we have created two buttons "Button 1" and "Button 2" to show the status of the button.

<Shot>
BoxLayout:
orientation: "horizontal"
size: root.width, root.height

Button:
id: but_one
text: "Button One"
on_press: root.disable_other()
size_hint: (1, 1)
disabled: False

Button:
id: but_two
text: "Button Two"
on_press: root.disable_other()
size_hint: (1, 1)
disabled: True
You can also try this code with Online Python Compiler
Run Code

 

Output:

 

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. 

After reading about this topic, are you not feeling excited to read/explore more articles on the topics of kivy python? Don’t worry, Coding ninjas has you covered, you can see Introduction to Kivy, Progress Widget Bar in KIvy, Animation in Kivy, and Canvas Widget in Kivy.To learn more on such topics, you can visit Coding Ninjas!

Refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, JavascriptSystem Design, and many more if you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass