加载时更新kivy中的标签文本

2024-04-26 21:26:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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更新它们,如下所示:

^{pr2}$

我正在考虑启动时钟,它调用update_txt函数,并且可以更改值,但是我一直得到错误的消息:ids不存在。。。等等,我是面向对象编程的新手,我搞不懂这个简单的事情


Tags: textselftruesizedsbuttonlabelcolor
1条回答
网友
1楼 · 发布于 2024-04-26 21:26:14

一些观察结果:

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

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

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

你的代码可以是:

  • 主.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:

  • 主.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
    

相关问题 更多 >