Random Color Kivy Button Generator

  • Share this:

Code introduction


This function creates a Kivy application that contains a button with a randomly generated color.


Technology Stack : Kivy, Button, BoxLayout, Random

Code Type : Kivy Application

Code Difficulty : Intermediate


                
                    
def random_color_button(text, size=(100, 50)):
    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.boxlayout import BoxLayout
    from random import randint

    class RandomColorApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical')
            button = Button(text=text, size=size)
            button.color = (randint(0, 1), randint(0, 1), randint(0, 1), 1)
            layout.add_widget(button)
            return layout

    RandomColorApp().run()