【发布时间】:2018-07-28 15:21:44
【问题描述】:
过去 6 周左右一直在玩 python,过去两周我一直在尝试学习 kivy。很难弄清楚如何更新我用来在我的应用中显示时间的标签。
我已经在这里查看了粗制时钟,非常粗制,一切都很好,但它返回一个标签而不是根小部件:
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
import time
class IncrediblyCrudeClock(Label):
def update(self, *args):
self.text = time.asctime()
class TimeApp(App):
def build(self):
crudeclock = IncrediblyCrudeClock()
Clock.schedule_interval(crudeclock.update, 1)
return crudeclock
if __name__ == "__main__":
TimeApp().run()
到目前为止,这是我的代码:
from kivy.app import App
from kivy.config import Config
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import time
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
global clocktext
class v3Widget(BoxLayout):
pass
class ClockText(Label):
def update(self, *args):
self.text = time.strftime('%I'+':'+'%M'+' %p')
return ClockText
class v3App(App):
def build(self):
clocktext = ClockText()
Clock.schedule_interval(clocktext.update, 1)
return v3Widget()
if __name__=="__main__":
v3App().run()
这是基维:
<v3Widget>
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: 1,0.85
Label:
text: App.ClockText
size_hint: 0.75,1
Button:
text: 'not clicked'
size_hint: 0.25,1
BoxLayout:
orientation: 'horizontal'
size_hint: 1,0.15
Button:
text: '1'
Button:
text: '2'
Button:
text: '3'
Button:
text: '4'
Button:
text: '5'
任何关于如何让时钟标签每分钟更新一次的建议将不胜感激。我能得到的最接近的是将变量设置为clocktext并让它在启动时加载但不会改变。
提前致谢
【问题讨论】: