gpt4 book ai didi

python - 使用 Builder.load_string 在 Kivy 应用程序上显示变量

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:34 24 4
gpt4 key购买 nike

如何在 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'])

关于python - 使用 Builder.load_string 在 Kivy 应用程序上显示变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55567535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com