简体   繁体   中英

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. What am i missing?

.py file:


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

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 is displaying 2 Labels but they are overwritten or super-imposed ie one is in the background and the other is in the foreground.

Soltion

Add a BoxLayout to separate the two Labels .

Snippets - 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:
    text_size: root.width, None
    size: self.texture_size

Solution

  • Remove text_size: root.width, None and size: self.texture_size .
  • Add 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. This will also automatically wrap the text of the Label to remain within this area.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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