我是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函数的时钟,这可能会改变值,但我不断得到错误,即id不存在......等等,我是面向对象编程的新手,我可以算出这个简单的事情
答案 0 :(得分:1)
对于在 kivy 中寻找 on_load 事件而找到解决此问题的方法的任何人,我都找到了解决方案。 kivy 中的 App 类提供了一个成员函数 on_start(self),一旦应用程序完成构建和加载,就会调用该函数。我们只需要在应用程序加载完毕后添加我们想要调用的函数。 例如:
class MyApp(App):
def build(self):
...
def on_start(self):
super().on_start(self)
my_f()
update_texts()
答案 1 :(得分:0)
一些观察结果:
作为评论@eyllanesc,您不应将子类命名为它继承的类。
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属性而不是id:
<强> 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