You can download this code by clicking the button below.
This code is now available for download.
This function creates a Kivy application with a button. Each time the button is clicked, it changes the button's background color to a random color.
Technology Stack : Kivy, random, randint
Code Type : Kivy App
Code Difficulty : Intermediate
def random_color_button(text="Random Color", size=(100, 50)):
from kivy.app import App
from kivy.uix.button import Button
from random import randint
from kivy.uix.boxlayout import BoxLayout
class RandomColorApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.button = Button(text=text, size=size)
self.button.bind(on_press=self.change_color)
layout.add_widget(self.button)
return layout
def change_color(self, instance):
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
instance.background_color = [r / 255, g / 255, b / 255, 1]
return RandomColorApp().run()