python - 如何每秒更新 NumericProperty?

标签 python python-3.x kivy

我有三个 NumericProperteis,我想每秒更新一次。我尝试使用 Clock.schedule_interval(),因为它与我想要的相似。如何更新 NumericProperteis?我可以使用另一个在 Kivy MainLoop 中运行的事件吗?或者我没有正确更改 NumericProperty?

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.clock import Clock


Builder.load_string("""
<MyLabel>
    text: "{}: {}".format(self.title, self.value)

<UpdatingLabels>
    GridLayout:
        rows: 1
        pos: 0, 0
        size: root.size
        MyLabel:
            id: lb0
            title: "value 0"
        MyLabel:
            id: lb1
            title: "value 1"
        MyLabel:
            id: lb2
            title: "value 2"
""")


class UpdatingLabels(Widget):
    pass


class MyLabel(Label):
    value = NumericProperty(0)
    title = StringProperty('')

# This not working but the same what i wanna do
# In real case i have values genereted every second
# def clock_def(dt):
#     MyLabel.ids.lb0.value += 1 # or MyLabel.ids.lb0.value = genereted_value_1
#     MyLabel.ids.lb1.value += 2
#     MyLabel.ids.lb2.value += 3

# event = Clock.schedule_interval(clock_def, 1)


class MyApp(App):
    def build(self):

        return UpdatingLabels()


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

最佳答案

“id”是实例的属性,而不是类的属性,所以如果你想访问它们,请在类中使用创建的对象或自身。因此您无法使用 MyLabel 访问 ID。

解决方案是在 UpdatingLabels 类中实现逻辑。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.clock import Clock


Builder.load_string(
    """
<MyLabel>
    text: "{}: {}".format(self.title, self.value)

<UpdatingLabels>
    GridLayout:
        rows: 1
        pos: 0, 0
        size: root.size
        MyLabel:
            id: lb0
            title: "value 0"
        MyLabel:
            id: lb1
            title: "value 1"
        MyLabel:
            id: lb2
            title: "value 2"
"""
)


class UpdatingLabels(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        event = Clock.schedule_interval(self.clock_def, 1)

    def clock_def(self, dt):
        self.ids.lb0.value += 1
        self.ids.lb1.value += 2
        self.ids.lb2.value += 3


class MyLabel(Label):
    value = NumericProperty(0)
    title = StringProperty("")


class MyApp(App):
    def build(self):
        return UpdatingLabels()


if __name__ == "__main__":
    MyApp().run()

关于python - 如何每秒更新 NumericProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505075/

相关文章:

python - 将按钮绑定(bind)到 .kv 文件中 .py 中的类方法

python - Kivy - 有什么方法可以将 Python 函数绑定(bind)到使用 kv 语言创建的小部件吗?

python - kivy: python for android + C++

python - 仅读取某些行的文本文件以在 Python 中拆分文件

python - 从 Python+Pyserial 向 Arduino 发送串行数据时出错

python - 如何在 python 中对多个数据框进行迭代步骤?

python3如何将unicode代码点转换为unicode char

python-3.x - 从 ._sparsetools 导入 csr_tocsc、csr_tobsr、csr_count_blocks、\ImportError : DLL load failed: The specified module could not be found

python - 向量化时保留文本数据的顺序

忽略附加参数的 Python 实例化对象