Kivy用python更改标签文本

时间:2016-12-19 09:53:25

标签: python label kivy updating

我用Python半熟,但对Kivy来说是全新的,我知道我的问题是引用了标签ID,但我似乎无法解决它并且搜索似乎没有给我我需要什么。

我试图获得一个标签来显示当前时间,所以我知道我在更新等方面有正确的框架,但我确定这一切都归结为以某种方式引用标签ID及其我挣扎着?

以下代码运行正常,显示标签等,直到我尝试更新label_text.text,此时我收到错误:AttributeError: 'float' object has no attribute 'lbl_time'。我已经尝试str(time.strftime("%H:%M:%S")),但这并没有解决它。

from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition, FadeTransition
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import ObjectProperty, StringProperty
from kivy.clock import Clock
import time
from datetime import datetime

class MainScreen(Screen):

def update_time(self):
    lbl_time = ObjectProperty()
    MyTime = time.strftime("%H:%M:%S")
    self.lbl_time.text = MyTime

class DetailScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

class MyScreenManager(ScreenManager):
    pass



root_widget = Builder.load_string('''
    MyScreenManager:
    MainScreen:
    DetailScreen:
    SettingsScreen:

<MainScreen>:
    name: 'main'
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: lbl_time
            text: 'Time' 
            font_size: 60
        Label:
            text: 'Main2'
            font_size: 30
        GridLayout:
            cols: 2
            Label:
                text: 'Bottom'
                font_size: 30
            Label:
                text: 'Bottom1'
                font_size: 30

 <DetailScreen>:
     name: 'details'

 <SettingsScreen>:
     name: 'settings'

 ''')
 class ScreenManagerApp(App):

     def build(self):
         return root_widget

     def on_start(self):
         Clock.schedule_interval(MainScreen.update_time, 1)

 ScreenManagerApp().run()

1 个答案:

答案 0 :(得分:0)

这更像是一个Python问题,而不是一个Kivy问题。您正在调用类update_time类的MainScreen,而不是MainScreen的对象/实例。基本上,您需要在build方法中保存对象的引用(self.main_screen),然后在on_start中使用它。

class ScreenManagerApp(App):

    def build(self):
        self.main_screen = MainScreen()
        return self.main_screen

    def on_start(self):
        Clock.schedule_interval(self.main_screen.update_time, 1)

此外,您无法访问kv语言之外的id,即在python中。您必须通过添加属性来引用ID,例如the_time

<MainScreen>:
    name: 'main'
    the_time: _id_lbl_time
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: _id_lbl_time

一个小问题是update_time()接收参数。此外,代码中有很多奇怪的东西,所以我无法运行代码。我在下面的代码中修正了以上所有内容:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.clock import Clock
import time
from datetime import datetime

Builder.load_string('''
<MainScreen>:
    name: 'main'
    the_time: _id_lbl_time
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: _id_lbl_time
            text: 'Time' 
            font_size: 60
 ''')

class MainScreen(Screen):

    def update_time(self, sec):
        MyTime = time.strftime("%H:%M:%S")
        self.the_time.text = MyTime


class ScreenManagerApp(App):

    def build(self):
        self.main_screen = MainScreen()
        return self.main_screen

    def on_start(self):
        Clock.schedule_interval(self.main_screen.update_time, 1)

ScreenManagerApp().run()