简体   繁体   中英

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

I am trying to display variable archon2_channel from the function label_title as an MDLabel text value. I have tried to use StringProperty(var_name) , also through the global variables, no luck... appreciated for any ideas and or links that can help to solve it.

I am placing this function in the App class:

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 file:

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

Hmm it actually just happened )) here is what i did, i removed code from function label_title() and put it in a class DemoApp(MDApp) level:

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']

in KV file:

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

Easier than i thought!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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