簡體   English   中英

如何每秒更新 NumericProperty?

[英]How to update NumericProperty every second?

我有三個 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”是實例的屬性,而不是 class 的屬性,因此如果要訪問它們,請使用創建的 object 或 class 中的 self。 所以你不能使用 MyLabel 訪問 id。

解決方案是在 UpdatingLabels class 中實現邏輯。

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()

Eyllanesc 的解決方案是正確的,但它當然不是唯一的,如果您想保留應用程序上的邏輯,另一種選擇是使用根小部件中的 id。

由於您的根 class 是 UpdatingLabels 並且您想要更新其中具有 id 的小部件,您可以通過 app.root 訪問它們,它始終引用構建方法返回的小部件。

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('')




class MyApp(App):
    def build(self):
        Clock.schedule_interval(self.clock_def, 1)
        return UpdatingLabels()

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM