【问题标题】:Kivy not updating the labelKivy 没有更新标签
【发布时间】:2020-12-03 20:41:50
【问题描述】:

我想我已经准备好解决所有类似的问题,但无法解决这个问题。

我正在尝试通过 python 函数更新在 .kv 文件中定义的标签。 我要更新的标签是 lbl_autohours

我已经尝试过 StringProperty,如下所示,直接引用 (self.ids.lbl_autohours.text = "test123"),如果我通过单击按钮调用它,它就可以工作。但是一旦收到一些数据,我想从 .py-script 调用它。我无法让它工作。

...
# 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
...

【问题讨论】:

    标签: python kivy label kivy-language


    【解决方案1】:

    您引用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 时也要小心

    【讨论】:

    • 没区别,还是不更新。我怀疑这与我调用函数的方式有关?
    • 我尝试在 Screen_177 的 init 函数中运行 self.add_widget(Label(text="test22")) ,效果很好。但如果我在 showData 函数中运行它,它就不起作用了。
    • 它确实有效!看看我在你的showData中所做的更改
    • 看看我在loadTimeseries方法中所做的改动
    • 我收到此错误:文件“c:\Users\username\Google Drive\Python\ALOG\ALOG-app\1204_main.py”,第 61 行,在 loadTimeseries self.parent.ids.screen_177 .showData(timeseries) A​​ttributeError: 'NoneType' 对象没有属性 'ids'
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多