如何使自己在猕猴桃中自动更新时钟标签? [英] how to make self updating clock label in kivy?

查看:52
本文介绍了如何使自己在猕猴桃中自动更新时钟标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个充当时钟并每秒更新一次的标签,就像 链接,但在状态栏中.

I want to make a Label which acts as a clock and gets updated every second just as in making-a-clock-in-kivy link but in the status bar.

我希望 status.kv 文件中带有 id:_tnd 的标签充当时钟. 更新函数(test_gui.py)中的print语句确实起作用,并且每隔一秒钟便在控制台中打印日期和时间,但是标签没有得到更新.我现在很困惑!这可能是一个愚蠢的错误,但是我该怎么做?

I want the label in status.kv file with id: _tnd to act as a clock. The print statement in the update function (test_gui.py) does work and prints the date and time in the console after every second, but the label is not getting updated. I am pretty much confused right now!. It might be a silly mistake, but How do I do it?

我有3个文件

  1. test_gui.py
  2. test.kv
  3. status.kv

test_gui.py文件

test_gui.py file

import time
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty

Builder.load_file('status.kv')

class Status(BoxLayout):
    _change = StringProperty()
    _tnd = ObjectProperty(None)
    def update(self,*args):
        self.time = time.asctime()
        self._change = str(self.time)
        self._tnd.text = str(self.time)
        print self._change

class C(BoxLayout):
    pass


class TimeApp(App):
    def build(self):
        self.load_kv('test.kv')
        crudeclock = Status()
        Clock.schedule_interval(crudeclock.update, 1)
        return C()

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

test.kv文件

    <C>:
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            Button:
                text: "Button"
            Label:
                text: "Label"
        Status:

status.kv文件

status.kv file

    <Status>:
    size_hint: 1,.1
    _tnd: _tnd
    canvas.before:
        Color:
            rgba: 0,0,0,1
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text:'Current Date and Time:'
    Label:
        id: _tnd
        text: root._change +' time'

推荐答案

您的代码存在一些问题.最大的是您的build(self)函数:

There are a few issues with your code. The biggest is in your build(self) function:

def build(self):
    self.load_kv('test.kv')
    crudeclock = Status()
    Clock.schedule_interval(crudeclock.update, 1)
    return C()

您正在创建一个Status对象并设置一个时钟来调用它的更新功能,但是它不是显示屏的一部分.它是Status的单独的独立实例,未附加到窗口小部件树.返回C()时,它将使用自己的内部Status实例(未更新)创建在test.kv中定义的小部件树.

You are creating a Status object and setting up a clock to call it's update function, but it is not part of your display. It is a separate, independent instance of Status that is not attached to your widget tree. When you return C(), it creates the widget tree defined in test.kv with its own, internal Status instance that is not being updated.

第二个问题是您将Label的文本字段绑定到.kv文件中的属性,然后还在回调中手动更改它.我猜您尝试过一种,然后再尝试另一种,看看是否可行.如果您使用正确的对象,那么两种方法都可以使用,但是您只想使用一个.

The second issue is that you are binding the Label's text field to a property in the .kv file, and then also manually changing it in the callback. I'm guessing you tried one and then the other to see if either worked. Both will work if you are using the right object, but you only want to use one.

就访问正确的Status对象而言,修复代码的最简单方法是在test.kv中对其进行标记,然后在build(self)中进行访问:

As far as accessing the right Status object, the simplest way to fix your code is to tag it in test.kv, then access it in build(self):

<C>:
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            Button:
                text: "Button"
            Label:
                text: "Label"
        Status:
            id: stat

和:

def build(self):
    self.load_kv('test.kv')
    c = C()
    stat = c.ids.stat  # this is the right object
    Clock.schedule_interval(stat.update, 1)
    return c

另一种选择,因为您实际上只需要为整个应用程序保留一次时间,所以将属性放入您的应用程序类中并绑定到kv文件中:

An alternative, since you really only need to keep time once for your whole app, is to put the property in your app class and bind to it in the kv file:

    time = StringProperty()

    def update(self, *args):
        self.time = str(time.asctime()) # + 'time'?

    def build(self):
        self.load_kv('test.kv')
        Clock.schedule_interval(self.update, 1)
        return C()

<Status>:
    size_hint: 1,.1
    canvas.before:
        Color:
            rgba: 0,0,0,1
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text:'Current Date and Time:'
    Label:
        text: app.time

看起来更干净一点.

这篇关于如何使自己在猕猴桃中自动更新时钟标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆