简体   繁体   English

如何在屏幕上更新标签的文字 - Kivy

[英]How to update a label's text in a screen - Kivy

I've been trying to make a program that continuously updates and displays the readings obtained from a sensor. 我一直在尝试制作一个程序,不断更新并显示从传感器获得的读数。 However, after the sensor value is updated, the label still displays the old values in the background. 但是,更新传感器值后,标签仍会在后台显示旧值。 How do i clear the screen before the new values are displayed? 如何在显示新值之前清除屏幕?

I've tried the self.clear_screen but that didn't help. 我已经尝试过self.clear_screen但这并没有帮助。 What am i missing? 我错过了什么?

.py file: .py文件:


class MainScreen(Screen):
   pass



class ScreenThermo(Screen):
    def __init__(self,**kwargs):
        super(ScreenThermo, self).__init__(**kwargs)
        Clock.schedule_interval(self.getTemp, 2)


    def getTemp(self,dt):
        temperature = sensor.get_temperature()
        thetemp = temperature 
        self.manager.screen_thermo.ids.TempLabel.text = str(thetemp)


    def on_enter(self, *args):
        self.__init__()

    pass

class ScreenManagement(ScreenManager):
   pass



.kv file .kv文件

ScreenManagement:    
    id: screen_manager
    screen_thermo: screen_thermo
    MainScreen:
    ScreenThermo:
        id: screen_thermo
        name: 'thermo'
        manager: screen_manager

<MainScreen>:
    name: "main"
    Label:
        text: "Welcome to \n Interactive HealthCare \n System"
        font_size: 60
        halign: 'center'
        valign: 'middle'
        pos_hint: {'x': .01, 'y': .05}
        on_touch_down: app.root.current = "thermo"



<ScreenThermo>:
    Label:
        text: "temperature"
        font_size: 60
        text_size: root.width, None
        size: self.texture_size
        halign: 'left'
        valign: 'middle'
    Label:
        id: TempLabel
        text: "temperature updated"
        font_size: 60
        text_size: root.width, None
        size: self.texture_size
        halign: 'center'
        valign: 'middle'

ScreenThermo - Labels Overwritten ScreenThermo - 覆盖的标签

ScreenThermo is displaying 2 Labels but they are overwritten or super-imposed ie one is in the background and the other is in the foreground. ScreenThermo显示2个标签,但它们被覆盖或叠加,即一个在后台,另一个在前景。

Soltion Soltion

Add a BoxLayout to separate the two Labels . 添加BoxLayout以分隔两个Labels

Snippets - kv 片段 - kv

<ScreenThermo>:
    BoxLayout:
        Label:
            text: "temperature"
            font_size: 60
            text_size: self.size
            halign: 'left'
            valign: 'middle'
        Label:
            id: TempLabel
            text: "temperature updated"
            font_size: 60
            text_size: self.size
            halign: 'center'
            valign: 'middle'

Root Cause - label still displays the old values 根本原因 - 标签仍显示旧值

The old values are being displayed because the following create a Label that can grow vertically but wraps the text at a certain width. 正在显示旧值,因为以下内容创建了一个可以垂直增长但将文本包装在一定宽度的Label。

Label:
    text_size: root.width, None
    size: self.texture_size

Solution

  • Remove text_size: root.width, None and size: self.texture_size . 删除text_size: root.width, Nonesize: self.texture_size
  • Add text_size: self.size . 添加text_size: self.size

Example

The following code binds this size to the size of the Label, so text will be aligned within the widget bounds. 以下代码将此大小绑定到Label的大小,因此文本将在小部件边界内对齐。 This will also automatically wrap the text of the Label to remain within this area. 这也将自动包装Label的文本以保留在此区域内。

Label:
    text_size: self.size
    halign: 'right'
    valign: 'middle'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM