Random Text Generator App in Kivy

  • Share this:

Code introduction


This code creates a Kivy application with a random text generator. It uses a vertical layout (BoxLayout) and two labels (Label) to display random text.


Technology Stack : Kivy

Code Type : Kivy Application

Code Difficulty : Intermediate


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

class RandomWidget(Widget):
    def __init__(self, **kwargs):
        super(RandomWidget, self).__init__(**kwargs)
        self.layout = BoxLayout(orientation='vertical')
        self.add_widget(self.layout)
        self.generate_random_text()

    def generate_random_text(self):
        self.layout.clear_widgets()
        self.layout.add_widget(Label(text=choice(['Hello', 'World', 'Kivy', 'Python', 'Code'])))
        self.layout.add_widget(Label(text=choice(['This', 'Is', 'A', 'Random', 'Widget'])))

class RandomApp(App):
    def build(self):
        return RandomWidget()

if __name__ == '__main__':
    RandomApp().run()                
              
Tags: