Skip to content Skip to sidebar Skip to footer

How To Replace Kivy Widgets On Callback?

I'm new to Python and Kivy, and I'm trying to create multipage display of letters of the braille alphabet, with the corresponding braille's letter picture present in every page. I

Solution 1:

Here is a simple solution to your problem. I'll leave it to you to modify and make it look and work exactly how you want :)

Learning the kv language is INCREDIBLY helpful, easy, and it can be picked up quite quickly.

main.py

from kivy.app import App

classMainApp(App):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'defnext_letter(self):
        # Get a reference to the widget that shows the letters# self.root refers to the root widget of the kv file -- in this case,# the GridLayout
        current_letter_widget = self.root.ids['the_letter_label']
        # Get the letter currently shown
        current_letter = current_letter_widget.text
        # Find the next letter in the alphabet
        next_letter_index = self.alphabet.find(current_letter) + 1
        next_letter = self.alphabet[next_letter_index]

        # Set the new letter in the widget that shows the letters
        current_letter_widget.text = next_letter

MainApp().run()

main.kv

GridLayout:# This is the `root` widget of the main app classcols:1Label:text:"g"id:the_letter_label# Setting an id for a widget lets you refer to it laterButton:text:"Previous"Button:text:"Next"on_release:# the keyword `app` references the main app class, so we can call# the `next_letter` functionapp.next_letter()

I'm happy to address specific questions if you have them.

Post a Comment for "How To Replace Kivy Widgets On Callback?"