Random Color Generator Kivy App

  • Share this:

Code introduction


This code defines a Kivy application that randomly generates a color and displays it on the interface.


Technology Stack : Kivy, random, randint

Code Type : Kivy App

Code Difficulty : Intermediate


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

    class RandomColorApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical')
            self.label = Label(text='', size_hint=(1, 0.1))
            layout.add_widget(self.label)
            self.update_color()
            return layout

        def update_color(self):
            red = randint(0, 255)
            green = randint(0, 255)
            blue = randint(0, 255)
            color = f'#{red:02x}{green:02x}{blue:02x}'
            self.label.text = f'Random Color: {color}'
            self.label.color = (red / 255.0, green / 255.0, blue / 255.0, 1)

    if __name__ == '__main__':
        RandomColorApp().run()