为什么我似乎无法将标签放入 Kivy 应用程序中的 StackLayout(或其他布局)中?

tex*_*exb 2 python kivy

理论上,将 Kivy- Label放入堆栈布局(或其他布局)应该是最简单、最常见的事情 - 但它目前让我陷入绝望。以下语法显示一个普通的单按钮应用程序(在 Kivy 1.8 或 1.9、Win 7 下),直到我取消注释 Label-generateing line,该行始终失败并显示诸如AttributeError: 'LabelPygame' object has no attribute 'bind'或 之类的消息AttributeError: 'LabelSDL2' object has no attribute 'bind'(在 中layout.py):

from kivy.app import App
from kivy.uix.button import Button
from kivy.core.text import Label
from kivy.uix.stacklayout import StackLayout

class TestApp(App):
    def build(self):
        mylayout = StackLayout(orientation='lr-tb')
        mylayout.add_widget(Button(text='This button can always be rendered.'))
        # mylayout.add_widget(Label(text='This label seems to cause trouble.'))
        return mylayout

TestApp().run()
Run Code Online (Sandbox Code Playgroud)

我有一种预感,我忽略了一些非常明显或愚蠢的东西,但无法理解它是什么。基于Builderand的替代调用runTouchApp似乎工作得很好(布局类型似乎没有什么区别):

# ... other imports abbreviated ... 

Builder.load_string('''
<MyLayout>:
    Button:
        text: "This button can always be rendered."
    Label:
        text: "This label works in this case."
''')

class MyLayout(FloatLayout):
    pass

runTouchApp(MyLayout())
Run Code Online (Sandbox Code Playgroud)

inc*_*ent 5

您正在进口kivy.core.text.Label,但您确实想要kivy.uix.label.Label

  • 这很痛,但是当疼痛减轻时真是太好了!无论如何,切换到“from kivy.uix.xyzlayout import XyzLayout”解决了问题。感谢您指出我的错误的本质,这可能会让我免于再盯着屏幕几个小时...... (2认同)