2017-08-13 8 views
2

私はkivyを使用してコンピュータ支援学習のための小さなアプリケーションを作成しています。Kivy:任意のウィジェットから設定値にアクセス

現時点では、設定値にアクセスする際にいくつか問題があります。 私はメインのAppクラスで

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

で値を取得します。それはうまくいきますが、別のウィジェット(この場合はAudioButton)内でこれらの値にアクセスする方法はわかりません。

私はScreenを含むScreenManagerを使用しています。内部にはBoxLayoutがあり、これにはGridLayoutが含まれており、これにはいくつかのAudioButtonがあります。

ここで、このAudioButtonでは、mainAppに定義されているself.languageの現在の値を知りたいと思います。

.kvファイルでは、私は

`text: app.language` 

のようなものは、それを得るために行うことができますが、どのようにPythonで直接それを行うには?

kvでダミーのラベルを使用して値を取得すると機能しますが、設定を変更すると、on_config_change()に追加する必要があることがわからないため、アプリケーションを再起動する必要があります値。

ここでは、私のアプリケーションのすべての興味深い部分が非常に単純化されたバージョンです、私は願っています。

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.config, 
           data=settings_json) 

    def on_config_change(self, config, section, 
         key, value): 
     if key == 'language': 
      self.language = value 
     if key == 'playsound': 
      self.playsound = value 


if __name__ == "__main__": 
    LunahutsoApp().run() 

そして.kvファイル:

<ScreenManagement>: 
    MainScreen: 
    Lesson1: 

<AudioButton>: 
    Label: 
     id: language 
     text: app.language 
     color: 0, 0, 0, 0 
    Label: 
     id: playsound 
     text: app.playsound 
     color: 0, 0, 0, 0 

<MainScreen>: 
    name: "main" 
    BoxLayout: 
     orientation: 'vertical' 
     Button: 
      on_release: app.root.current = "lesson1" 
      text: "Lesson" 
      font_size: 50 
     Button: 
      on_release: app.open_settings() 
      text: "Settings" 
      font_size: 50 
     Button: 
      on_release: sys.exit() 
      text: "Quit" 
      font_size: 50 

<Lesson1>: 
    name: "lesson1" 
    id: lesson1 
    BoxLayout: 
     orientation: 'vertical' 
     Pictures1: 
      size_hint_y: 0.5 
     BoxLayout: 
      size_hint_y: 0.15 
      Label: 
       text: "" 

答えて

1

あなたはアプリクラスget_running_app()の次のメソッドを使用することができ、あなたはを通して別のクラスからの設定を参照することができ、ここでhttps://kivy.org/docs/api-kivy.app.html#kivy.app.App.get_running_app

この方法を参照してくださいappクラス。

私は以下の簡単な例を書きました。私はself.text = App.get_running_app().config.get('Label','content')を使用してconfigのsthにアクセスしています。

from kivy.app import App 
from kivy.uix.textinput import TextInput 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.lang import Builder 
from kivy.uix.label import Label 
from kivy.config import Config 


class Labelwithconfig(Label): 

    def check_label(self): 
     self.text = App.get_running_app().config.get('Label','content') 

kv_str = Builder.load_string(""" 
BoxLayout: 
    orientation: 'vertical' 
    Labelwithconfig: 
     id: labelconf 
    Button: 
     text: 'open settings' 
     on_press: app.open_settings() 
""") 



class MyApp(App): 
    def build_config(self, config): 
     config.setdefaults('Label', {'Content': "Default label text"}) 

    def build_settings(self, settings): 
     settings.add_json_panel("StackOverflow Test Settings", self.config, data=""" 
     [ 
     {"type": "options", 
     "title": "Label text System", 
     "section": "Label", 
     "key": "Content", 
     "options": ["Default label text", "Other Label text"] 
     } 
     ]""" 
     ) 
    def on_config_change(self, config, section, key, value): 
     self.root.ids.labelconf.check_label() 

    def build(self): 
     return kv_str 


if __name__ == '__main__': 
    MyApp().run() 
関連する問題