【发布时间】:2019-04-27 00:34:50
【问题描述】:
我有 schedule_interval 调用一个从 web 获取天气数据然后将其解析为 dict 的函数。我有我的 kv 文件读取该字典并在浮动布局中显示值。我知道该函数正在被调用,因为我也将它打印到控制台,但它没有在 floatlayout 窗口中更新。我认为这些值会根据我阅读的内容自动更新。
GUI.py
class weather(FloatLayout):
def w(self):
a = parse()
print(a)
return a
class weatherApp(App):
def build(self):
d = weather()
Clock.schedule_interval(d.w, 1)
return d
weather.kv
<Layout>:
DragLabel:
font_size: 600
size_hint: 0.1, 0.1
pos: 415,455
text: str(root.w()['temp0'])
这只是标签之一。我对 Kivy 很陌生,所以 如果这对你有经验的 kivy 来说看起来很糟糕 各位,我很抱歉。
def w(self) 的 print(a) 部分:每秒工作一次,但窗口不显示新变量。
test.py
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
a = {}
a['num'] = 0
class test(FloatLayout):
def w(self):
a['num'] += 1
print(a['num'])
return a
class testApp(App):
def build(self):
d = test()
Clock.schedule_interval(test.w, 1)
return d
if __name__ == '__main__':
p = testApp()
p.run()
test.kv
#:kivy 1.10.1
<Layout>:
Label:
font_size: 200
size_hint: 0.1, 0.1
pos: 415,455
text: str(root.w()['num'])
【问题讨论】:
-
谢谢,我只是添加了一个test.py和test.kv的形式