【问题标题】:How to dynamically update a Kivy label from a continuously running function?如何从连续运行的函数中动态更新 Kivy 标签?
【发布时间】:2021-02-26 20:48:10
【问题描述】:

我试图让一个函数连续运行并吐出标签使用的距离,我最终将绑定到声纳模块,但标签仍然空白,我不知道我做错了什么.如果我只是为该距离变量添加一个打印语句,它会很好地打印和更新,只是无法让标签使用它。

我的问题的第二部分是如何在第二个窗口中引用我的同一个函数并且还有一个从同一个函数更新的标签?

提前感谢您的帮助,我对kivy非常陌生,几个月前也刚开始学习python。

Python 代码:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen  # for multiple screens
from kivy.properties import StringProperty


class MySonar(Screen):
    global i
    i = 1

    distance = StringProperty("")

    #Generic function that just adds itself up, just using to try and get the label to change before I throw in my real function
    def sonar(self):
        global i

        if i < 250:
            distance = (10 + .1 * i)
            i += 1

        else:
            i = 1
            distance = 10

        self.root.distance=str(distance)

class DropWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("help.kv")

class HelpMe(App):
    def build(self):

        #running interval update to keep running code above
        Clock.schedule_interval(lambda dt: MySonar.sonar(self), 0.1)

        return kv

if __name__ == "__main__":
    HelpMe().run()

基维:

WindowManager:
    MySonar:
    DropWindow:

<MySonar>:
    name:"Main"

    GridLayout:
        cols:1

        ##Need this to update
        Label:
            text:root.distance
        Button:
            text:"Next Window"
            on_release:
                app.root.current="Drop"
                root.manager.transition.direction="left"


<DropWindow>:
    name:"Drop"

    GridLayout:

        cols:1

        ##Need this to update, commented out the text so the program will run and you can see the blank label for part one of my question
        Label:
            ##text:root.distance

        Button:
            text:"Cancel"
            on_release:
                app.root.current="Main"
                root.manager.transition.direction="right"

【问题讨论】:

    标签: python dynamic kivy label window


    【解决方案1】:

    为了简化对distance 的访问,您可以将StringProperty 放在HelpMe App 中:

    class MySonar(Screen):
        global i
        i = 1
    
        #Generic function that just adds itself up, just using to try and get the label to change before I throw in my real function
        def sonar(self):
            global i
    
            if i < 250:
                distance = (10 + .1 * i)
                i += 1
    
            else:
                i = 1
                distance = 10
    
            # set the value of distance in the StringProperty of the App
            App.get_running_app().distance=str(distance)
            print(distance)
    
    class DropWindow(Screen):
        pass
    
    class WindowManager(ScreenManager):
        pass
    
    # kv = Builder.load_file("help.kv")
    
    
    class HelpMe(App):
        distance = StringProperty('')
    
        def build(self):
            kv = Builder.load_file("help.kv")
    
            #running interval update to keep running code above
            sonar_instance = kv.get_screen('Main')
            Clock.schedule_interval(lambda dt: sonar_instance.sonar(), 0.1)
    
            return kv
    
    if __name__ == "__main__":
        HelpMe().run()
    

    请注意,我还将Builder.load_file() 移到了App 中。当您在 kv 文件中引用 app 时,这是一个很好的做法(正如我所做的那样)。此外,使用MySonar.sonar(self) 调用sonar() 方法将不起作用。您需要使用对 GUI 中 MySonar 实例的引用。

    现在kv 文件变为:

    WindowManager:
        MySonar:
        DropWindow:
    
    <MySonar>:
        name:"Main"
    
        GridLayout:
            cols:1
    
            ##Need this to update
            Label:
                text: app.distance
            Button:
                text:"Next Window"
                on_release:
                    app.root.current="Drop"
                    root.manager.transition.direction="left"
    
    
    <DropWindow>:
        name:"Drop"
    
        GridLayout:
    
            cols:1
    
            ##Need this to update, commented out the text so the program will run and you can see the blank label for part one of my question
            Label:
                text: app.distance
    
            Button:
                text:"Cancel"
                on_release:
                    app.root.current="Main"
                    root.manager.transition.direction="right"
    

    变化在于Labelstext 属性现在只是app.distance

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多