Kivy没有更新标签

2024-06-26 13:48:56 发布

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

我想我已经准备好解决所有类似的问题,但无法让它发挥作用

我试图通过python函数更新.kv文件中定义的标签。 我正在尝试更新的标签是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
...

Tags: textselfidinitdefscreenkwargsclass
1条回答
网友
1楼 · 发布于 2024-06-26 13:48:56

你引用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引用它,您的代码将恢复运行,并且在命名idsproperties时要小心

相关问题 更多 >