我试图制作一个多屏幕应用程序,我希望所有这些都有一个日期/时间标签。 我做了一个时钟事件,每秒更新一次文本值。
时钟事件正在运行,因为在python控制台上我可以看到它更新,但标签没有刷新,它只是第一次工作。 如何刷新屏幕?
main.py文件:
import kivy
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.clock import Clock
kivy.require("1.9.1")
Window.size = (1280,1024)
Config.set('graphics', 'fullscreen', '0')
class TimeLabel(Label):
def __init__(self, **kwargs):
super(TimeLabel, self).__init__(**kwargs)
self.text= str(time.asctime())
def update(self, *args):
self.text = str(time.asctime())
print self.text
class LoginScreen(Screen):
pass
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class My1App(App):
def build(self):
my1sm = Builder.load_file("main.kv")
crudeclock = TimeLabel()
Clock.schedule_interval(crudeclock.update, 1)
return my1sm
if __name__ == '__main__':
My1App().run()
和main.kv文件:
#: kivy 1.9
#: import ScreenManager kivy.uix.screenmanager.ScreenManager
#: import Screen kivy.uix.screenmanager.ScreenManager
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
LoginScreen:
<MainScreen>:
name: 'main'
size_hint: (1, 1)
FloatLayout:
Image:
source: './background.png'
size: 1280,1024
TimeLabel:
<LoginScreen>:
name: 'loginS'
FloatLayout:
Image:
source: './Login.png'
size: 1280,1024
TimeLabel:
<TimeLabel>:
x:-545
y:-475
color: (0,0,0,1)
答案 0 :(得分:1)
您需要在实例化标签时设置schedule_interval。
import kivy
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.clock import Clock
kivy.require("1.9.1")
Window.size = (600,600)
Config.set('graphics', 'fullscreen', '0')
class TimeLabel(Label):
def __init__(self, **kwargs):
super(TimeLabel, self).__init__(**kwargs)
self.text= str(time.asctime())
Clock.schedule_interval(self.update,1)
def update(self, *args):
self.text = str(time.asctime())
print self.text
class LoginScreen(Screen):
pass
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class My1App(App):
def build(self):
my1sm = Builder.load_file("main.kv")
crudeclock = TimeLabel()
return my1sm
if __name__ == '__main__':
My1App().run()