在过去的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()
这是Kivy:
<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并在启动时加载它但不会改变。
提前致谢
答案 0 :(得分:0)
您将错误的实例发送到Clock.interval
。您应该发送在kv file
class ClockText(Label):
def update(self, *args):
self.text = time.strftime('%I'+':'+'%M'+' %p')
def build(self):
v3w = v3Widget()
clocktext = v3w.ids.clocktext
Clock.schedule_interval(clocktext.update, 1)
return v3w
还在kv文件中创建一个ID:
<ClockText>:
<v3Widget>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: 1,0.85
ClockText: # <--------- !!!
id: 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'
答案 1 :(得分:0)
显示时间的标签应该是ClockText
类的实例。您有几种可能性,例如:
<强> main.py 强>:
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.properties import ObjectProperty
import time
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
class V3Widget(BoxLayout):
clock_label = ObjectProperty()
def __init__(self, **kwargs):
super(V3Widget, self).__init__(**kwargs)
Clock.schedule_interval(self.clock_label.update, 1)
class ClockLabel(Label):
def update(self, *args):
self.text = time.strftime('%I:%M %p')
class V3App(App):
def build(self):
return V3Widget()
if __name__ == "__main__":
V3App().run()
<强> v3.kv 强>:
<V3Widget>
clock_label: clock_label
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: 1,0.85
ClockLabel:
id: clock_label
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'
如果标签仅显示时间,您也可以自行更新标签:
main.py (与上例相同的kv):
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
import time
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
class V3Widget(BoxLayout):
pass
class ClockLabel(Label):
def __init__(self, **kwargs):
super(ClockLabel, self).__init__(**kwargs)
Clock.schedule_interval(self.update, 1)
def update(self, *args):
self.text = time.strftime('%I:%M %p')
class V3App(App):
def build(self):
return V3Widget()
if __name__ == "__main__":
V3App().run()
您不需要使用全局变量,但是您做错了:要在函数内部使用全局变量,您需要在函数/方法中执行global <varName>
强>,而不是你声明它。