Kivy用python更改标签文本 [英] Kivy change label text with python
问题描述
我对Python不太满意,但对于Kivy来说是全新的,我知道我的问题是引用标签ID,但是我似乎无法解决它,搜索似乎也无法满足我的需求.
I'm semi-OK with Python but brand new to Kivy, I know my problem is referencing the label ID but I can't seem to solve it and searching doesn't seem to give me what I need.
我正在尝试获取一个标签来显示当前时间,所以我知道我在更新等方面拥有正确的框架,但是我敢肯定,这全都取决于以某种方式引用标签ID的原因,挣扎着吗?
I'm trying to get a label to display the current time, so I know I have the right framework in terms of updating etc but I'm sure its all down to referencing the label ID somehow and its that I'm struggling with?
以下代码运行良好,显示标签等,直到我尝试更新label_text.text
时出现错误:AttributeError: 'float' object has no attribute 'lbl_time'
.我已经尝试过str(time.strftime("%H:%M:%S"))
,但是并不能解决问题.
The following code runs fine, displays the labels etc until I try to update the label_text.text
at which point i get an error: AttributeError: 'float' object has no attribute 'lbl_time'
. I've tried str(time.strftime("%H:%M:%S"))
but that doesn't solve it.
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()
推荐答案
这更多是Python问题,而不是Kivy问题.您正在调用类MainScreen
类的update_time
,而不是MainScreen
的对象/实例.基本上,您需要在build
方法中保存对对象(self.main_screen)的引用,然后在on_start中使用它.
This was more of a Python problem rather than a Kivy one. You were calling the update_time
of the class MainScreen
class, not of the object/instance of the MainScreen
. Basically, you would need to save a reference to the object (self.main_screen) in the build
method, and then use it in the 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语言之外(即在python中)访问id.您必须通过添加属性来引用ID,例如the_time
:
Also you cannot access id outside of the kv language, i.e. in the python. You have to reference the id by adding a property, e.g. the_time
:
<MainScreen>:
name: 'main'
the_time: _id_lbl_time
BoxLayout:
orientation: 'vertical'
Label:
id: _id_lbl_time
一个小问题是update_time()
接收到参数.另外,您的代码中有很多怪异的东西,因此我无法运行该代码.我在以下代码中修复了以上所有问题:
A minor problem is that the update_time()
receives to parameters. Also, there were quite a few weird things in your code, so I couldn't run the code. I fixed all the above in the code below:
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()
这篇关于Kivy用python更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!