我试图让一个函数连续运行,并吐出标签使用的距离,最终我将连接到声纳模块,但标签仍然空白,我不知道我做错了什么。如果我只是为那个距离变量添加一个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"
要简化对}中:
distance
的访问,可以将StringProperty
放在HelpMe
{请注意,我还将
Builder.load_file()
移动到了App
中。当您在kv
文件中引用app
时,这是一个很好的实践(正如我所做的)。另外,使用MySonar.sonar(self)
调用sonar()
方法将不起作用。您需要使用对GUI中的MySonar
实例的引用现在
kv
文件变成:变化是两个
Labels
的text
属性现在只是app.distance
相关问题 更多 >
编程相关推荐