【问题标题】:kivyMD how to call a function from builder?kivyMD 如何从构建器调用函数?
【发布时间】:2021-09-08 00:49:50
【问题描述】:

我是 kivyMD 的新手,请有人帮忙解决这个问题。

我有两个文件:一个是主python文件,另一个是builder文件。

我想从 builder 文件中调用一个名为 show_data 的函数。

有人知道调用函数的正确方法吗?

这是 main.py 文件

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from helpers import username_input,loginButton

class PrototypeChikis(MDApp):

    def build(self):

        screen = Screen()
        self.theme_cls.primary_palette = "Blue"

        self.username = Builder.load_string(username_input)
        self.button = Builder.load_string(loginButton)
        screen.add_widget(self.username)
        screen.add_widget(self.button)
        return screen

    def show_data(self,obj):
        print(self.username.text)

PrototypeChikis().run()

这是千伏。文件

username_input = """
MDTextField:
    hint_text: "Enter username"
    icon_right: "tanker-truck"
    icon_right_color: app.theme_cls.primary_color
    pos_hint: {'center_x':0.5,'center_y': 0.5}
    size_hint_x:None
    width:300
    
"""

loginButton = """
MDRectangleFlatButton:
    text: 'Log In'
    pos_hint: {'center_x':0.5, 'center_y':0.4}
    on_press: self.show_data()   
"""

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    你可以使用一些功能,你可以复制粘贴我刚刚修改的代码,我把所有东西都放在一个文件中:

    from kivymd.app import MDApp
    from kivymd.uix.screen import Screen
    from kivy.lang import Builder
    try:
        from helpers import username_input,loginButton
    except:
        pass
    username_input = """
    MDTextField:
        id: texto_jbsidis
        hint_text: "Enter username"
        icon_right: "tanker-truck"
        icon_right_color: app.theme_cls.primary_color
        pos_hint: {'center_x':0.5,'center_y': 0.5}
        size_hint_x:None
        width:300
        on_text: app.show_while_typing(self.text)
        
    """
    
    loginButton = """
    MDRectangleFlatButton:
        text: 'Log In'
        pos_hint: {'center_x':0.5, 'center_y':0.4}
        on_press: app.show_data()   
    """
    
    class PrototypeChikisjbsidis(MDApp):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.screen = Screen()
        def build(self):
    
            
            self.theme_cls.primary_palette = "Blue"
    
            self.username = Builder.load_string(username_input)
            self.button = Builder.load_string(loginButton)
            self.screen.add_widget(self.username)
            self.screen.add_widget(self.button)
            return self.screen
    
        def show_while_typing(self,the_text_from_the_jbsidis_text_field):
            print(len(the_text_from_the_jbsidis_text_field),the_text_from_the_jbsidis_text_field)
    
        def show_data(self):
            """
            MDRectangleFlatButton:
                text: 'Log In'
                pos_hint: {'center_x':0.5, 'center_y':0.4}
                on_press: app.show_data()   
            """
            t="self.screen.children[1] is MDTextField texto_jbsidis "
            Textfield_23=self.screen.children[1]
            the_jbsidis_text_is=Textfield_23.text
            print(the_jbsidis_text_is)
    
    PrototypeChikisjbsidis().run()
    

    图片,jbsidis:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 2020-03-12
      • 2015-06-13
      • 2021-10-21
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多