在Load上更新Kivy中的标签文本



我是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不存在的错误...等等,我是针对目标的编程的新手,我可以't这个简单的东西

对于任何在搜索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()

一些观察:

  • 作为注释@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属性代替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
    

最新更新