Button with Click-to-Change Background Color

  • Share this:

Code introduction


This function creates a button with specified text and color. When the button is clicked, the window background color will change to white.


Technology Stack : Kivy

Code Type : Kivy UI component

Code Difficulty : Intermediate


                
                    
def random_button_click(text, color):
    from kivy.uix.button import Button
    from kivy.core.window import Window

    # Create a button with the given text and color
    button = Button(text=text, color=color)
    
    # Bind the button click event to a lambda function
    button.bind(on_press=lambda instance: Window.clear_color((1, 1, 1, 1)))
    
    return button                
              
Tags: