[英]Kivy not updating the label
我想我已經准備好解決所有類似的問題,但無法讓它發揮作用。
我正在嘗試通過 python function 更新在.kv 文件中定義的 label。 我要更新的 label 是 lbl_autohours
我已經嘗試過 StringProperty,如下所示,直接引用 (self.ids.lbl_autohours.text = "test123"),如果我通過單擊按鈕調用它,它就可以工作。 但是一旦收到一些數據,我想從 .py 腳本中調用它。 我無法讓它發揮作用。
...
# Welcome screen
class WelcomeScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.updateInputField, 0.1)
...
def loadTimeseries(self, assetToShow, aspect):
timeseries = get_timeseries(clientid, clientsecret, payload, assetToShow, aspect, latestValue="true")
print("Done")
mainApp.current = 'scr_177'
screen_177.showData(timeseries)
...
class Screen_177(Screen):
lbl_autohours = StringProperty()
def __init__(self, **kwargs):
super(Screen_177, self).__init__(**kwargs)
self.lbl_autohours="123" #Works
def showData(self, timeseries):
print("Running showData")
print(timeseries[0]['Auto_hours'])
self.lbl_autohours = "test123" #Doesnt work
...
mainApp = Builder.load_file("alogconn.kv")
# Main app
class myApp(App):
def build(self):
return mainApp
# Run main program
if __name__ == "__main__":
Window.clearcolor = (1, 1, 1, 1)
screen_177 = Screen_177()
my_app = myApp()
my_app.run()
...
.KV 文件:
ScreenManagement:
id: screen_manager
transition: WipeTransition(clearcolor=(1,1,1,1))
welcomeScreen: scr_welcome
loadingScreen: scr_loading
screen177: scr_177
WelcomeScreen:
id: scr_welcome
LoadingScreen:
id: scr_loading
Screen_177:
id: scr_177
...
<Screen_177>:
clearcolor: (1,1,1,1)
name: 'scr_177'
Image:
source: 'logo.png'
size_hint: 0.5, 0.5
pos_hint: {"center_x":0.5, "top":1}
Label:
text: '177'
font_size: 30
size_hint: 1, 0.2
pos_hint: {"center_x":0.5, "top":0.5}
GridLayout:
cols: 2
id: data_grid
Label:
text: "Auto hours:"
Label:
id: lbl_autohours
text: root.lbl_autohours
...
你引用lbl_autohours
的方式不太合適(我認為)試試這個
# Welcome screen
class WelcomeScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.updateInputField, 0.1)
...
def loadTimeseries(self, assetToShow, aspect):
timeseries = get_timeseries(clientid, clientsecret, payload, assetToShow, aspect, latestValue="true")
print("Done")
mainApp.current = 'scr_177'
self.parent.ids.screen_177.showData(timeseries)
...
class Screen_177(Screen):
label_auto = StringProperty()
def __init__(self, **kwargs):
super(Screen_177, self).__init__(**kwargs)
self.label_auto = "123"
def showData(self, timeseries):
print("Running showData")
print(timeseries[0]['Auto_hours'])
self.ids.lbl_autohours.text = "test123"
...
mainApp = Builder.load_file("alogconn.kv")
# Main app
class myApp(App):
def build(self):
return mainApp
# Run main program
if __name__ == "__main__":
Window.clearcolor = (1, 1, 1, 1)
screen_177 = Screen_177()
my_app = myApp()
my_app.run()
...
和這個
ScreenManagement:
id: screen_manager
transition: WipeTransition(clearcolor=(1,1,1,1))
welcomeScreen: scr_welcome
loadingScreen: scr_loading
screen177: scr_177
WelcomeScreen:
id: scr_welcome
LoadingScreen:
id: scr_loading
Screen_177:
id: scr_177
...
<Screen_177>:
clearcolor: (1,1,1,1)
name: 'scr_177'
Image:
source: 'logo.png'
size_hint: 0.5, 0.5
pos_hint: {"center_x":0.5, "top":1}
Label:
text: '177'
font_size: 30
size_hint: 1, 0.2
pos_hint: {"center_x":0.5, "top":0.5}
GridLayout:
cols: 2
id: data_grid
Label:
text: "Auto hours:"
Label:
id: lbl_autohours
text: root.label_atuo
由於lbl_autohours
已經存在於Screen_177
中,為什么不使用ids
引用它,您的代碼將重新生效,並且在命名ids
和properties
時也要小心
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.