加载时更新 kivy 中的标签文本
updating label text in kivy on load
我是 kivy 的新手,正在尝试为我的树莓派构建小型 OSD。
我的 .kv 文件如下所示:
BoxLayout:
orientation: 'vertical'
Label:
text_size: self.size
text: 'OSD'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
GridLayout:
cols: 2
Label:
text_size: self.size
text: 'Total entries in DB: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: total_db
text_size: self.size
text: '366 000 '
font_size: 30
bold: True
color: 0, 1, 0, 1
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Info 1: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: marked_update
text_size: self.size
text: '1328 '
color: 1, 0, 0, 1
font_size: 30
bold: True
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Activity'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
Label:
text: ''
font_size: 10
halign: 'center'
valign: 'top'
size_hint: 1, .08
GridLayout:
cols: 4
Button:
text: 'DS 01'
font_size: 25
background_color: 1, 0, 0, 1
Button:
text: 'DS 02'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 03'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 04'
font_size: 25
background_color: 0, 1, 0, 1
这会产生我想要的外观。我想定期更新带有 ID 的两个标签文本以及我稍后提取的值...但我什至无法从 python 更新它们,如下所示:
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
class BoxLayout(BoxLayout):
def __init__(self, **kwargs):
super(BoxLayout, self).__init__(**kwargs)
Clock.schedule_once(self.update_txt, 0)
def update_txt(self, *args):
self.label.ids.marked_update.txt='updated from python'
class osdApp(App):
def build(self):
self.title = 'OSD'
return BoxLayout()
if __name__ == '__main__':
osdApp().run()
我正在考虑启动调用 update_txt 函数的时钟,这可能会改变值,但我一直收到错误,ids 不存在......等等,我是新手面向对象编程,我搞不懂这个简单的事情
几点观察:
正如@eyllanesc 的评论,你不应该将你的子 class 命名为它继承的 class。
self.label.ids.marked_update.txt
不正确。应该是self.ids.marked_update.text
.
将您的根小部件声明为 kv 规则。
您的代码可以是:
main.py
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
super(BoxLayout, self).__init__(**kwargs)
Clock.schedule_once(self.update_txt, 0.1)
def update_txt(self, *args):
self.ids.marked_update.text = 'updated from python'
class OsdApp(App):
def build(self):
self.title = 'OSD'
return RootWidget()
if __name__ == '__main__':
OsdApp().run()
osd.kv:
<RootWidget>:
orientation: 'vertical'
Label:
text_size: self.size
text: 'OSD'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
GridLayout:
cols: 2
Label:
text_size: self.size
text: 'Total entries in DB: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: total_db
text_size: self.size
text: '366 000 '
font_size: 30
bold: True
color: 0, 1, 0, 1
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Info 1: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: marked_update
text_size: self.size
text: '1328 '
color: 1, 0, 0, 1
font_size: 30
bold: True
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Activity'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
Label:
text: ''
font_size: 10
halign: 'center'
valign: 'top'
size_hint: 1, .08
GridLayout:
cols: 4
Button:
text: 'DS 01'
font_size: 25
background_color: 1, 0, 0, 1
Button:
text: 'DS 02'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 03'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 04'
font_size: 25
background_color: 0, 1, 0, 1
但是,我建议使用 kivy 属性而不是 ids:
main.py:
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class RootWidget(BoxLayout):
marked_text = StringProperty()
def __init__(self, **kwargs):
super(BoxLayout, self).__init__(**kwargs)
self.update_txt()
def update_txt(self, *args):
self.marked_text ='updated from python'
class OsdApp(App):
def build(self):
self.title = 'OSD'
return RootWidget()
if __name__ == '__main__':
OsdApp().run()
osd.kv:
<RootWidget>:
id: root_layout
orientation: 'vertical'
marked_text: '1328 '
Label:
text_size: self.size
text: 'OSD'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
GridLayout:
cols: 2
Label:
text_size: self.size
text: 'Total entries in DB: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: total_db
text_size: self.size
text: '366 000 '
font_size: 30
bold: True
color: 0, 1, 0, 1
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Info 1: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: marked_update
text_size: self.size
text: root_layout.marked_text
color: 1, 0, 0, 1
font_size: 30
bold: True
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Activity'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
Label:
text: ''
font_size: 10
halign: 'center'
valign: 'top'
size_hint: 1, .08
GridLayout:
cols: 4
Button:
text: 'DS 01'
font_size: 25
background_color: 1, 0, 0, 1
Button:
text: 'DS 02'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 03'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 04'
font_size: 25
background_color: 0, 1, 0, 1
对于在 kivy 中搜索 on_load 事件而找到解决此问题方法的任何人,我已经找到了解决方案。
kivy 中的 App class 提供了一个成员函数 on_start(self),一旦应用程序完成构建和加载就会调用它。我们只需要在应用程序完成加载后在其主体中添加我们想要调用的函数。
例如:
class MyApp(App):
def build(self):
...
def on_start(self):
super().on_start(self)
my_f()
update_texts()
我是 kivy 的新手,正在尝试为我的树莓派构建小型 OSD。
我的 .kv 文件如下所示:
BoxLayout:
orientation: 'vertical'
Label:
text_size: self.size
text: 'OSD'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
GridLayout:
cols: 2
Label:
text_size: self.size
text: 'Total entries in DB: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: total_db
text_size: self.size
text: '366 000 '
font_size: 30
bold: True
color: 0, 1, 0, 1
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Info 1: '
font_size: 30
bold: False
halign: 'left'
size_hint: 1, .1
Label:
id: marked_update
text_size: self.size
text: '1328 '
color: 1, 0, 0, 1
font_size: 30
bold: True
halign: 'center'
size_hint: 1, .1
Label:
text_size: self.size
text: 'Activity'
font_size: 50
bold: True
halign: 'center'
valign: 'top'
size_hint: 1, .3
Label:
text: ''
font_size: 10
halign: 'center'
valign: 'top'
size_hint: 1, .08
GridLayout:
cols: 4
Button:
text: 'DS 01'
font_size: 25
background_color: 1, 0, 0, 1
Button:
text: 'DS 02'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 03'
font_size: 25
background_color: 0, 1, 0, 1
Button:
text: 'DS 04'
font_size: 25
background_color: 0, 1, 0, 1
这会产生我想要的外观。我想定期更新带有 ID 的两个标签文本以及我稍后提取的值...但我什至无法从 python 更新它们,如下所示:
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
class BoxLayout(BoxLayout):
def __init__(self, **kwargs):
super(BoxLayout, self).__init__(**kwargs)
Clock.schedule_once(self.update_txt, 0)
def update_txt(self, *args):
self.label.ids.marked_update.txt='updated from python'
class osdApp(App):
def build(self):
self.title = 'OSD'
return BoxLayout()
if __name__ == '__main__':
osdApp().run()
我正在考虑启动调用 update_txt 函数的时钟,这可能会改变值,但我一直收到错误,ids 不存在......等等,我是新手面向对象编程,我搞不懂这个简单的事情
几点观察:
正如@eyllanesc 的评论,你不应该将你的子 class 命名为它继承的 class。
self.label.ids.marked_update.txt
不正确。应该是self.ids.marked_update.text
.将您的根小部件声明为 kv 规则。
您的代码可以是:
main.py
import kivy kivy.require('1.10.0') from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.clock import Clock class RootWidget(BoxLayout): def __init__(self, **kwargs): super(BoxLayout, self).__init__(**kwargs) Clock.schedule_once(self.update_txt, 0.1) def update_txt(self, *args): self.ids.marked_update.text = 'updated from python' class OsdApp(App): def build(self): self.title = 'OSD' return RootWidget() if __name__ == '__main__': OsdApp().run()
osd.kv:
<RootWidget>: orientation: 'vertical' Label: text_size: self.size text: 'OSD' font_size: 50 bold: True halign: 'center' valign: 'top' size_hint: 1, .3 GridLayout: cols: 2 Label: text_size: self.size text: 'Total entries in DB: ' font_size: 30 bold: False halign: 'left' size_hint: 1, .1 Label: id: total_db text_size: self.size text: '366 000 ' font_size: 30 bold: True color: 0, 1, 0, 1 halign: 'center' size_hint: 1, .1 Label: text_size: self.size text: 'Info 1: ' font_size: 30 bold: False halign: 'left' size_hint: 1, .1 Label: id: marked_update text_size: self.size text: '1328 ' color: 1, 0, 0, 1 font_size: 30 bold: True halign: 'center' size_hint: 1, .1 Label: text_size: self.size text: 'Activity' font_size: 50 bold: True halign: 'center' valign: 'top' size_hint: 1, .3 Label: text: '' font_size: 10 halign: 'center' valign: 'top' size_hint: 1, .08 GridLayout: cols: 4 Button: text: 'DS 01' font_size: 25 background_color: 1, 0, 0, 1 Button: text: 'DS 02' font_size: 25 background_color: 0, 1, 0, 1 Button: text: 'DS 03' font_size: 25 background_color: 0, 1, 0, 1 Button: text: 'DS 04' font_size: 25 background_color: 0, 1, 0, 1
但是,我建议使用 kivy 属性而不是 ids:
main.py:
import kivy kivy.require('1.10.0') from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.properties import StringProperty class RootWidget(BoxLayout): marked_text = StringProperty() def __init__(self, **kwargs): super(BoxLayout, self).__init__(**kwargs) self.update_txt() def update_txt(self, *args): self.marked_text ='updated from python' class OsdApp(App): def build(self): self.title = 'OSD' return RootWidget() if __name__ == '__main__': OsdApp().run()
osd.kv:
<RootWidget>: id: root_layout orientation: 'vertical' marked_text: '1328 ' Label: text_size: self.size text: 'OSD' font_size: 50 bold: True halign: 'center' valign: 'top' size_hint: 1, .3 GridLayout: cols: 2 Label: text_size: self.size text: 'Total entries in DB: ' font_size: 30 bold: False halign: 'left' size_hint: 1, .1 Label: id: total_db text_size: self.size text: '366 000 ' font_size: 30 bold: True color: 0, 1, 0, 1 halign: 'center' size_hint: 1, .1 Label: text_size: self.size text: 'Info 1: ' font_size: 30 bold: False halign: 'left' size_hint: 1, .1 Label: id: marked_update text_size: self.size text: root_layout.marked_text color: 1, 0, 0, 1 font_size: 30 bold: True halign: 'center' size_hint: 1, .1 Label: text_size: self.size text: 'Activity' font_size: 50 bold: True halign: 'center' valign: 'top' size_hint: 1, .3 Label: text: '' font_size: 10 halign: 'center' valign: 'top' size_hint: 1, .08 GridLayout: cols: 4 Button: text: 'DS 01' font_size: 25 background_color: 1, 0, 0, 1 Button: text: 'DS 02' font_size: 25 background_color: 0, 1, 0, 1 Button: text: 'DS 03' font_size: 25 background_color: 0, 1, 0, 1 Button: text: 'DS 04' font_size: 25 background_color: 0, 1, 0, 1
对于在 kivy 中搜索 on_load 事件而找到解决此问题方法的任何人,我已经找到了解决方案。 kivy 中的 App class 提供了一个成员函数 on_start(self),一旦应用程序完成构建和加载就会调用它。我们只需要在应用程序完成加载后在其主体中添加我们想要调用的函数。 例如:
class MyApp(App):
def build(self):
...
def on_start(self):
super().on_start(self)
my_f()
update_texts()