2つの言語(ドイツ語と英語)と設定のAppを作成したいので、言語を切り替えることができます。 言語クラスはAppの一部であるため、app.Lanuageを使用してそれを評価する必要がありますが、上記のエラーメッセージが表示されます。kivyエラーNameError:グローバル名 'app'が定義されていません
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
from setjson import *
Builder.load_string('''
<Interface>:
orientation: 'vertical'
Button:
text: app.Lanuage.first_caption
font_size: 150
on_release: app.open_settings()
''')
class Interface(BoxLayout):
def __init__(self, **kwargs):
super(Interface, self).__init__(**kwargs)
self.test = app.Lanuage.all_button
class SettingsApp(App):
def build(self):
config = SettingsApp.get_running_app().config
language = config.getdefault("example", "optionsexample", "English").lower()
if language == 'english':
from lang_engl import Lang
if language == 'deutsch':
from lang_deutsch import Lang
self.Lanuage = Lang()
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
setting = self.config.get('example', 'boolexample')
return Interface()
def build_config(self, config):
config.setdefaults('example', {
'optionsexample': 'English',
'stringexample': 'some_string',
'pathexample': '/some/path'})
def build_settings(self, settings):
settings.add_json_panel('Panel Name',
self.config,
data=settings_json)
def on_config_change(self, config, section, key, value):
print 'value: ' + str(value)
SettingsApp().run()
あなたはスコープ内に 'App'しか持っていないので、' App.Language'を意味するか、 'kivy.app'モジュールを意味します。' kivy.app.Language'か 'from kivy import app ; app.Language'となります。実際に私はkivyパッケージにはいません。 kivyにLanguageクラス/モジュールがありますか? – syntonym
いいえ、私は自分で言語クラスを作成し、そのインスタンスをアプリケーションに追加しました。また、キビ言語では問題なく動作しましたが、Pythonでは正しく動作しませんでした。 –
ああ、現在のアプリケーションへの参照が必要です。 'App.get_running_app()' [documentation](https://kivy.org/docs/api-kivy.app.html#kivy.app.App.get_running_app)で取得できます。 – syntonym