我想制作一个应用程序,有两种语言(德语和英语)和设置,所以我可以在语言之间切换。 由于语言类是App的一部分,我需要使用app.Lanuage来处理它,但我收到了上面的错误消息。
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()
答案 0 :(得分:1)
示例:强>
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<MyBtn>:
text: str(self)+self.a.test
<Test>:
Button:
text: str(self)+app.test
MyBtn:
''')
class Test(BoxLayout):
pass
class MyBtn(Button):
def __init__(self, **kw):
super(MyBtn, self).__init__(**kw)
self.a = App.get_running_app()
class My(App):
test = '\nHi!'
def build(self):
return Test()
My().run()
app
关键字在kv
App.get_running_app()
self.<object>
类(App
内)build(self)
除此之外,如果您实际访问代码中的App
,我也不会发现您可能遇到的任何其他问题。