Kivy App with Random Color Buttons

  • Share this:

Code introduction


This code creates a simple Kivy application that displays a vertical layout of buttons with different colors. Users can click these buttons, and each button's color matches its text color.


Technology Stack : Kivy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from random import choice

def random_button_layout():
    class RandomButtonApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical')
            buttons = ['Red', 'Green', 'Blue', 'Yellow', 'Purple']
            for button_color in buttons:
                button = BoxLayout(size_hint=(0.5, 0.2), orientation='vertical')
                color_button = Button(text=button_color, background_color=button_color.lower())
                button.add_widget(color_button)
                layout.add_widget(button)
            return layout

    RandomButtonApp().run()                
              
Tags: