【发布时间】:2021-01-17 12:10:52
【问题描述】:
我正在尝试将函数 label_title 中的变量 archon2_channel 显示为 MDLabel 文本值。我尝试使用StringProperty(var_name),也通过全局变量,没有运气...感谢任何可以帮助解决它的想法和或链接。
我将这个函数放在 App 类中:
Python:
class DemoApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
self.standard_increment = STANDARD_INCREMENT
self.load_all_kv_files(os.path.join(self.directory, "libs", "uix", "kv",))
self.load_all_kv_files(os.path.join(self.directory, "libs", "uix", "uix_drawer", "kv"))
self.root_widget = RootWidget()
self.screen_manager = self.root_widget.ids.screen_manager
self.nav_drawer = self.root_widget.ids.navigation_drawer
return self.root_widget
def label_title(self):
url_archon2 = "http://weburrl"
response_archon2 = requests.request("GET", url_archon2, headers=headers, data = "")
archon2_channel = response_archon2.json()['items']['contentChannel']
archon2_ticker = response_archon2.json()['items']['messageScheduleName']
print(archon2_channel)
print(archon2_ticker)
return StringProperty(archon2_ticker)
DemoApp().run()
KV 文件:
MDLabel
text: app.archon2_channel
size_hint_y: None
height: self.texture_size[1]
padding: 0, "20dp"
halign: "center"
theme_text_color: "Primary"
【问题讨论】:
-
变量是否在应用范围内?你能分享更多代码吗?
-
@Goldwave 嘿,谢谢你!!!
-
“显示 json 的值”是什么意思?这个价值从何而来?您在此处显示的代码中这是哪个确切的变量?
-
不错!你试过this.archon2_channel response_archon2.json()['items']['contentChannel']?我对kivy不是很熟悉,但我试试
-
@Code-Apprentice 抱歉我应该指定更好,json 值基本上存储在 archon2_channel 变量中
标签: python json variables kivy kivymd