我想解决如何把一个时钟放在一个kivy应用程序,但有困难。kivy&python中的自动事件
我可以使它通过按钮工作..(即如果我点击按钮,它会更新标签上的时钟时间),但我似乎无法让它工作自动更新每秒。
任何帮助非常感谢!
事件行注释掉了,我可以使用一个简单的文件使用打印命令,但不能更改标签文本的上下文。它只是说, “失踪1周人的说法”
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文件:
<test>:
id:test
BoxLayout:
Label:
id: labelID
text:"label"
Button:
id: buttonID
text:"button"
on_release:test.showClock(1)
编辑:
错误读取:
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'
EDIT2:
后很多试验和期待在其他人的在线代码,我设法得到它的工作..虽然我不知道为什么或如何..任何解释将不胜感激,以帮助我的理解...
工作的代码是:
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()
始终提出完整的错误消息(Traceback)。 – furas
哪个元素有'缺少1个参数'的问题' - 检查文档中它有多少个参数。 – furas
错误如下:文件“C:\ Python34 \ lib \ site-packages \ kivy \ clock.py”,第406行,格式为 ret = callback(self._dt) TypeError:showClock()缺少1所需的位置参数:'dt' >>> – d4py