Kivy App with Color-Changing Button

  • Share this:

Code introduction


Create a Kivy application that includes a button, which changes its color when clicked.


Technology Stack : Kivy, Python

Code Type : Kivy App

Code Difficulty : Intermediate


                
                    
def random_color_button(text="Click Me", size=(200, 50), color=(1, 0, 0)):
    from kivy.app import App
    from kivy.uix.button import Button
    from random import random

    class RandomColorButtonApp(App):
        def build(self):
            button = Button(text=text, size=size, background_color=color)
            button.bind(on_press=self.change_color)
            return button

        def change_color(self, instance):
            r, g, b = random(), random(), random()
            instance.background_color = (r, g, b)

    RandomColorButtonApp().run()                
              
Tags: