【问题标题】:kivy error NameError: global name 'app' is not definedkivy错误NameError:未定义全局名称'app'
【发布时间】:2016-07-07 10:12:05
【问题描述】:

我想做一个应用程序,有两种语言(德语和英语)和设置,所以我可以在语言之间切换。 由于语言类是应用程序的一部分,我需要使用 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()

【问题讨论】:

  • 你只有 App 在范围内,所以你的意思是 App.Language 或者你的意思是 kivy.app 模块,那么它将是 kivy.app.Languagefrom kivy import app; app.Language。其实我的kivy包里都没有。你确定 kivy 有语言类/模块吗?
  • 不,我自己创建了语言类,并将它的一个实例添加到应用程序中。而且,它在 kivy 语言中工作得非常好,但在 python 中却不行
  • 啊,你想要一个对当前应用程序的引用。你可以通过App.get_running_app()documentation获得。
  • Interface实例化时没有运行App。

标签: android python python-2.7 kivy


【解决方案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中的应用
  • 如果你想在 python 中使用应用程序,请使用App.get_running_app()
  • App类内使用self.&lt;object&gt;(在build(self)内)

除此之外,如果您实际访问代码中的App,我看不出您可能遇到的任何其他问题。

【讨论】:

  • 为什么 App.get_running_app() 返回 None
  • @GilgameschvonUruk 你的意思是在App 上课吗?
  • @GilgameschvonUruk 我不太确定为什么它不适用于构建为return CLASS() 的类,但我认为该应用程序可用于其他类之后 它是完全实例化的,这意味着当你在 build() 方法中返回类时,App 类还有一些事情要做,直到它对“外部世界”可用并且你在业务完成之前调用你的类因此 App.get_running_app() == 暂时没有。
猜你喜欢
  • 2014-03-07
  • 2011-04-27
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2014-10-24
  • 2012-05-29
  • 2013-08-23
相关资源
最近更新 更多