kivy和python中的自动事件

2024-10-03 04:39:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试如何在kivy应用程序上安装时钟,但遇到了困难。在

我可以通过一个按钮让它工作。。(例如,如果我点击这个按钮,它会更新标签上的时钟时间)但我似乎不能让它每秒钟都自动更新。在

非常感谢任何帮助!在

事件行注释掉了,我可以用print命令处理一个简单的文件,但不能在更改标签文本的上下文中使用。上面写着“缺少一个参数”

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
import time
from kivy.clock import Clock


class test(BoxLayout):
    def showClock(self,dt):
         timey = (time.strftime("%H"+":"+"%M"+":"+"%S")),dt
         self.ids.labelID.text = (timey[0])
    event = Clock.schedule_interval(showClock, 1)




class question(App):
    def build(self):       
        return test()

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

KV文件:

^{pr2}$

编辑:

错误如下:

File "C:\Python34\lib\site-packages\kivy\clock.py", line 406, in tick
ret = callback(self._dt)
TypeError: showClock() missing 1 required positional argument: 'dt'

编辑2:

经过大量的实验和在线查看别人的代码后,我设法让它发挥作用。。虽然我不知道为什么或者怎么。。任何解释都将不胜感激,以帮助我理解。。。在

有效的代码是:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
import time
from kivy.clock import Clock


class test(BoxLayout):
    def __init__(self, **kwargs):
        super(test, self).__init__(**kwargs)
        Clock.schedule_interval(self.showClock, 1)

    def showClock(self, dt):
         timey = (time.strftime("%H"+":"+"%M"+":"+"%S")),dt
         self.ids.labelID.text = (timey[0])



class question(App):
    def build(self):       
        return test()

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

Tags: fromtestimportselfapptimedefdt