【问题标题】:updating label text in kivy on load在加载时更新 kivy 中的标签文本
【发布时间】: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


【解决方案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()

【讨论】:

    【解决方案2】:

    一些观察:

    • 作为 cmets @eyllanesc,您不应将子类命名为它继承的类。

    • self.label.ids.marked_update.txt 不正确。应该是self.ids.marked_update.text

    • 将根小部件声明为 kv 规则。

    您的代码可能是:

    • ma​​in.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:

    • ma​​in.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
      

    【讨论】:

    • 谢谢!我也没有注意到它的 self.ids.marked_update.text 而不是 self.ids.marked_update.txt ,因为我有...... :D 有什么特别的理由不使用 ids 吗? (我很高兴它终于工作了,并且可以继续试验,这让我发疯了:D)
    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多