【发布时间】:2018-07-26 12:24:22
【问题描述】:
我是 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 不存在的错误......等等,我是面向对象编程的新手,我可以不要把这个简单的事情弄清楚
【问题讨论】:
-
.kv 中的缩进很重要,根据我的看法,这不合适,您可以更正它。
-
@eyllanesc 是的,论坛在我粘贴它时对其进行了格式化,我已经对其进行了编辑以反映我的文件...但这可能不是问题,如果缩进错误,它将无法启动.. .
-
我理解你,但那个小问题可能会让我们认为这是问题所在,验证代码是否正确。 :D
-
您的代码中的一个错误是从一个类继承的类不应被称为与其继承的类相同的类:
class BoxLayout(BoxLayout):
标签: python kivy kivy-language