【问题标题】:Python - How to bind Button on_press on Kivy with multiple parametersPython - 如何在 Kivy 上使用多个参数绑定 Button on_press
【发布时间】:2013-11-05 19:10:12
【问题描述】:

我阅读了thisthat 标签。 我的函数回调有错误,标签有 NoneType。

class Root(FloatLayout):

    def callback( self, label, instance, *args ):
        ## Try to pass Button.text to the label,
        ## but type(label) is a NoneType
        label.text = instance.text # here
        print(label, type(label))


    def load_content(self):
        content = self.content

        for but in range(65, 67):
            content.add_widget(Button( text=chr(but),
                on_press = partial(self.callback, self.lbl),
                font_size=20 ))

    content = ObjectProperty(None)
    lbl = ObjectProperty(None)

【问题讨论】:

  • lbl 是否在您的 .kv 文件中定义为根定义的一部分?
  • 我搞错了。我在 Builder 的左侧声明了label,但从右侧调用了lbl<Root> label: lbl Label: id: lbl ...

标签: python kivy


【解决方案1】:

查看您作为标签值传入的内容。

你是否得到类似这样的堆栈:

def my_callback(label, instance):            
    label.text = instance.text

my_callback(None, None)

Traceback (most recent call last):
  File "***/vars.py", line 34, in <module>
    my_callback(None, None)
  File "***/vars.py", line 32, in my_callback
    label.text = instance.text
AttributeError: 'NoneType' object has no attribute 'text'

如果是这样,您的源标签是 NoneType,您需要跟踪它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    相关资源
    最近更新 更多