You can download this code by clicking the button below.
This code is now available for download.
This code creates a Kivy application that contains a button whose background color will change between random colors.
Technology Stack : Kivy, BoxLayout, Button, random
Code Type : Kivy app
Code Difficulty : Intermediate
def random_color_button(text, color=(1, 0, 0, 1)):
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from random import choice
def change_color(self, instance):
colors = [(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 1), (1, 0, 1, 1)]
instance.background_color = choice(colors)
class ColorfulButtonApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.button = Button(text=text, background_color=color)
layout.add_widget(self.button)
self.button.bind(background_color=change_color)
return layout
# Usage
# colorful_button_app = ColorfulButtonApp()
# colorful_button_app.run()