Python 如何从连续运行的函数动态更新Kivy标签?
我试图让一个函数连续运行,并吐出标签使用的距离,最终我将连接到声纳模块,但标签仍然空白,我不知道我做错了什么。如果我只是为那个距离变量添加一个print语句,它可以很好地打印和更新,只是无法让标签使用它 我的问题的第二部分是如何在第二个窗口中引用相同的函数,并具有从该函数更新的标签 提前感谢您的帮助,我对kivy非常陌生,几个月前才开始学习python Python代码:Python 如何从连续运行的函数动态更新Kivy标签?,python,dynamic,kivy,label,window,Python,Dynamic,Kivy,Label,Window,我试图让一个函数连续运行,并吐出标签使用的距离,最终我将连接到声纳模块,但标签仍然空白,我不知道我做错了什么。如果我只是为那个距离变量添加一个print语句,它可以很好地打印和更新,只是无法让标签使用它 我的问题的第二部分是如何在第二个窗口中引用相同的函数,并具有从该函数更新的标签 提前感谢您的帮助,我对kivy非常陌生,几个月前才开始学习python Python代码: from kivy.app import App from kivy.clock import Clock from kiv
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()
从kivy.app导入应用
从kivy.clock导入时钟
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕#用于多个屏幕
从kivy.properties导入StringProperty
MySonar类(屏幕):
全球i
i=1
距离=StringProperty(“”)
#泛型函数,它只是将自身相加,在我加入真正的函数之前,它只是用来尝试更改标签
def声纳(自身):
全球i
如果i<250:
距离=(10+1*i)
i+=1
其他:
i=1
距离=10
self.root.distance=str(距离)
类下拉窗口(屏幕):
通过
类WindowManager(屏幕管理器):
通过
kv=Builder.load_文件(“help.kv”)
类帮助我(应用程序):
def生成(自):
#运行间隔更新以保持上述代码的运行
时钟时间间隔(lambda dt:MySonar.sonar(self),0.1)
回流千伏
如果名称=“\uuuuu main\uuuuuuuu”:
HelpMe().run()
基维:
WindowManager:
迈索尔:
DropWindow:
:
名称:“主要”
网格布局:
科尔斯:1
##需要更新这个吗
标签:
文本:root.distance
按钮:
文本:“下一个窗口”
发布时:
app.root.current=“Drop”
root.manager.transition.direction=“left”
:
名称:“下降”
网格布局:
科尔斯:1
需要更新,注释文本,程序运行,你可以看到我的问题的第一部分空白标签。
标签:
##文本:root.distance
按钮:
文本:“取消”
发布时:
app.root.current=“Main”
root.manager.transition.direction=“右”
为了简化对距离的访问
,您可以将该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()
更改是两个标签的文本属性现在只是app.distance
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()
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"