如何从连续运行的函数动态更新Kivy标签?

2024-10-02 14:18:41 发布

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

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

我的问题的第二部分是如何在第二个窗口中引用相同的函数,并具有从该函数更新的标签

提前感谢您的帮助,我对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"

Tags: theto函数textfromimportselfapp
1条回答
网友
1楼 · 发布于 2024-10-02 14:18:41

要简化对distance的访问,可以将StringProperty放在HelpMe{}中:

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

相关问题 更多 >