[英]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
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个标签,但它们被覆盖或叠加,即一个在后台,另一个在前景。
Add a BoxLayout
to separate the two Labels
. 添加
BoxLayout
以分隔两个Labels
。
<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'
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
text_size: root.width, None
and size: self.texture_size
. text_size: root.width, None
和size: self.texture_size
。 text_size: self.size
. text_size: self.size
。 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.