Random Color Changer App

  • Share this:

Code introduction


This function creates a Kivy application that includes a label. When the user clicks on the label, the color of the label changes randomly.


Technology Stack : Kivy, random

Code Type : Kivy App

Code Difficulty : Intermediate


                
                    
def generate_random_color():
    import random
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label

    class ColorApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical')
            self.label = Label(text='Click to generate a random color', size_hint=(1, 0.2))
            layout.add_widget(self.label)
            self.label.bind(on_press=self.change_color)
            return layout

        def change_color(self, instance):
            random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
            self.label.color = random_color

    return ColorApp().run()                
              
Tags: