繁体   English   中英

如何在 kivy 应用程序的 KV 文件中显示来自 Python 的变量值

[英]How to display variable value from Python in KV file in kivy app

我试图将函数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"

嗯,它实际上只是发生了)) 这就是我所做的,我从函数 label_title() 中删除了代码并将其放在类 DemoApp(MDApp) 级别:

class DemoApp(MDApp):
    url_archon2 = "web url"
    r_dev_info_archon2 = requests.request("GET", url_archon2, headers=headers, data="")
    ch_numb_archon2 = r_dev_info_archon2.json()['items']['contentChannel']

在 KV 文件中:

MDLabel
    text: app.ch_numb_archon2
    size_hint_y: None
    height: self.texture_size[1]
    padding: 0, "20dp"
    halign: "center"
    theme_text_color: "Primary"

比我想象的要容易!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM