如何在Builder.load_string本身上显示dikt ['r_pi'],并在每次单击更新按钮时对其进行更新?
from kivy.core.window import Window
Window.fullscreen = 0
Window.size = (1000, 500)
from kivy.app import App
from kivy.lang import Builder
import json
import requests
from kivy.properties import ObjectProperty
class MyApp(App):
url = 'https://podluck-b3df6.firebaseio.com/.json'
auth_key = '2BKTZfCSuILkpIRKzDAIWOMkhaC5jIq069Eg4oLS'
def get(self):
request = requests.get(self.url + '?auth=' + self.auth_key)
dikt = request.json() #yes it IS a dict i checked
self.btn.text = "The pod is" + str(dikt['r_pi'])
print(dikt['r_pi'])
def build(self):
return KV
KV = Builder.load_string ("""
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
GridLayout:
cols:1
Button:
id: btn
GridLayout:
cols:1
Button:
text: 'Update'
on_release: app.get()
""")
if __name__ == '__main__':
MyApp().run()
我希望屏幕上显示“未占用”,这是我的Firebase数据库上“ r_pi”键的值,但我得到的却是以下内容:
“ MyApp”对象没有属性“ btn”
最佳答案
id是相对于所属树的,即您必须首先获取ScreenManager,在这种情况下,它是根,因此解决方案是:
def get(self):
request = requests.get(self.url + '?auth=' + self.auth_key)
dikt = request.json() #yes it IS a dict i checked
self.root.ids.btn.text = "The pod is" + str(dikt['r_pi'])