Random Color Button Kivy App

  • Share this:

Code introduction


Create a Kivy application that includes a button with a randomly generated background color each time the application is started.


Technology Stack : Kivy, Button, BoxLayout, Image, Random

Code Type : Kivy GUI application

Code Difficulty : Intermediate


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

    class ColorfulButtonApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical')
            button = Button(text=text, size=size)
            button.background_normal = ''
            button.background_color = (randint(0, 255)/255.0, randint(0, 255)/255.0, randint(0, 255)/255.0, 1)
            layout.add_widget(button)
            return layout

    return ColorfulButtonApp().run()