Kivy: Access configuration values from any widget

General Tech Learning Aids/Tools 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm using kivy to create a small App for computer aided learning.

At the moment I have some problems with accessing config values">values. I get the value with

self.language = self.config.get('basicsettings', 'language')

in the main App-Class. That works fine, however, I don't know how to access these value inside another widget - in this case the AudioButton.

I'm using a ScreenManager, which contains a Screen. Inside is a BoxLayout, which contains a GridLayout and this holds several AudioButtons.

Now, in this AudioButton I want to know the current value of self.language defined in the mainApp.

In .kv files I can do something like

`text: app.language`

to get it, but how to do it directly in Python?

If I use a dummy label in kv to get the value, it works, but when I change the setting, I need to restart the App, because I don't know what I need to add to on_config_change() to update the value during runtime.

Here's a very simplified version of my App with all interesting parts, I hope.

class AudioButton(Button):
    filename = StringProperty(None)
    sound = ObjectProperty(None, allownone=True)

    def on_press(self):
        if self.ids.playsound.text == '1':
            self.sound.play()
        else:
            print('NoSound')


class MainScreen(Screen):
    pass


class Pictures1(GridLayout):
    def __init__(self, **kwargs):
        super(Pictures1, self).__init__(**kwargs)
        self.cols = 2
        btn = AudioButton()
        self.add_widget(btn)
        btn = AudioButton()
        self.add_widget(btn)


class Lesson1(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


class LunahutsoApp(App):
    def build(self):
        self.settings_cls = SettingsWithSidebar
        self.use_kivy_settings = False
        self.language = self.config.get('basicsettings', 'language')
        self.playsound = self.config.get('basicsettings', 'playsound')
        return ScreenManagement()

    def build_config(self, config):
        config.setdefaults('basicsettings', {
            'language': 'austrian',
            'playsound': 1})

    def build_settings(self, settings):
        settings.add_json_panel('Lunahutso',
                                self.
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.