【问题标题】:Kivy Python very basic Binding of Label TextKivy Python 非常基本的标签文本绑定
【发布时间】:2015-04-24 04:56:48
【问题描述】:

我知道这可能是一个非常基本的问题,但是在我花了几个小时思考它之后,我仍然无法弄清楚。

我基本上只是想将标签的文本绑定到 python 代码中的变量。让我们称之为value。但是每次我运行循环时它都应该更新Clock.schedule_interval(RootWidget.update, 1.0/1.0)

这里是python,经过简化,所以它基本上只是时间,它也被打印出来只是为了看看它是否真的在工作。

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.core.window import Window
import time

class RootWidget(FloatLayout):


    def update(self, *args):
        value = time.time()
        print value
        self.ids.value_label.text = str(value)


class MainApp(App):

    def build(self):
        Window.size = (800, 480)
        r = RootWidget()
        Clock.schedule_interval(r.update, 1)
        print 'build running'
        return r

    def on_pause(self):
        return True

if __name__ == '__main__':
    MainApp().run()

kv 文件如下所示:

<RootWidget>:

Label:
    id: value_label
    text:
    y: 20.0
    x: 0.0
    width: 100.0
    italic: False
    height: 50.0

【问题讨论】:

    标签: python binding label kivy


    【解决方案1】:
    Clock.schedule_interval(RootWidget.update, 1.0/1.0)
    

    您需要安排实例的更新方法,而不是类本身。

    例如:

    r = RootWidget()
    Clock.schedule_interval(r.update, 1)
    return r
    

    默认情况下时钟会传递一些参数,所以你也应该声明更新方法来接受这些。如果您不想使用它们,那么您可以这样做:

    def update(self, *args):
        ...
    

    【讨论】:

    • 谢谢!但是我仍然收到以下错误:self.value_label.text = str(value) AttributeError: 'RootWidget' object has no attribute 'value_label'
    • 那是因为该属性确实不存在。您可以使用 self.ids.value_label 通过 id 访问标签
    • 非常感谢!我将其更改为 self.ids.value_label.text = str(value) 导致以下 AttributeError:AttributeError: 'super' object has no attribute '__getattr__'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多