我正在尝试如何在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()
函数showClock需要参数dt。你可以用“partial”来传递这个论点。在文档中有解释:https://kivy.org/docs/api-kivy.clock.html 希望这有帮助。在
相关问题 更多 >
编程相关推荐