【问题标题】:Kivy can't get text input using ScreenManagerKivy 无法使用 ScreenManager 获取文本输入
【发布时间】:2021-01-24 16:37:09
【问题描述】:

我正在做一个项目,我正在使用 kivy。

我想创建一个应用程序,我需要多个页面,所以我使用 ScreenManages。 我还需要在其中一个页面中获取用户输入并保存它,所以我使用 MDTextField 获取文本和一个按钮来保存数据。 当我按下按钮时,应用程序应该从文本字段中获取数据并将其保存在带有 sqlite3 的文件中,但是当我按下按钮时,它给了我一个非常奇怪的错误。 我试图在没有 ScreenManager 的情况下只重写应用程序的那个页面,它可以工作。 我怎样才能使它也与 ScreenManager 一起工作?

如何使用 MDTextField 和 ScreenManager 获取用户输入

我会展示几行代码让你更好地理解:

这是 Kivy 代码:

<AddWindow>:
    name: "add"
        
MDTextField:
    id: account_link
    hint_text: "Link"
    helper_text: "Insert the Link of the WebSite to enter in the website from this app"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5, "center_y": 0.8}
    size_hint_x: None
    width: 1200

这是从文本字段中获取数据的代码(当用户按下提交按钮时执行的那部分代码):

data = self.root.ids["account_link"].text

这是我按下按钮时遇到的错误:

data = self.root.ids["account_link"].text
KeyError: 'account_link'

【问题讨论】:

    标签: python python-3.x kivy kivy-language kivymd


    【解决方案1】:

    请注意,documentation 表示:

    ids 被添加到根部件的 ids 字典中。

    措辞不佳的文档,因为它们在其他地方将“根小部件”称为整个 GUI 的根。但在这种情况下,“根小部件”是定义 ids 的规则的根。在您的情况下,这可能是 AddWindow 规则(由于您的 kv sn-p 的缩进,不能 100% 确定)。如果是这种情况,那么您需要对出现在 GUI 中的 AddWindow 实例的引用:

    data = addwindow_instance.ids["account_link"].text
    

    没有看到更多你的代码,我只能猜测访问AddWindow实例的适当方法。

    添加完整的代码后,我现在可以为您提供帮助。这是您的 add_passwd() 方法的修改版本:

    def add_passwd(self):
    
        # get a reference to the AddWindow Screen
        addwindow_instance = self.root.get_screen('add')
    
        # use that instance to access the MDTextFields
        account_link = addwindow_instance.ids["account_link"].text
        account_name = addwindow_instance.ids["md_account_name"].text
        account_nickname = addwindow_instance.ids["md_account_nickname"].text
        email = addwindow_instance.ids["md_email"].text
        passwd = addwindow_instance.ids["md_passwd"].text
    
        #TEST
        print(account_link)
        print(account_name)
        print(account_nickname)
        print(email)
        print(passwd)
    

    请注意,这还需要对您的kv 进行一些更正。无论你在哪里有类似的东西:

    id: "some_id"
    

    应该改为:

    id: some_id
    

    一个例子是id: "md_account_name"

    【讨论】:

    • 感谢您的帮助,非常感谢。但是我可以问你更多信息吗?我对kivy真的很陌生,而且我不太擅长理解文档。例如,我应该向 AddWindow 添加和实例(如何像在您的示例中那样创建 che addwindow_instance )?提前感谢您的帮助
    • 正如我所说,如果没有看到您的更多代码,我真的无法提出任何进一步的建议。请发帖minimal reproducible example
    • 好的,我会发布更多代码,例如答案,让您更好地理解我的问题
    • 非常感谢您的帮助我终于解决了我的问题,谢谢!这对我来说真的很重要。非常感谢!
    【解决方案2】:

    这是我的更多代码:

    # Screens
    class MainWindow(Screen):
        pass
    class AddWindow(Screen):
        pass
    class WindowManager(ScreenManager):
        pass
    
    KV = """
    WindowManager:
    MainWindow:
    AddWindow:
    
    <MainWindow>:
        name: "main"
    
    MDRoundFlatButton:
        text: "Add"
        pos_hint: {"center_x": 0.5, "center_y": 0.7}
        on_press: 
            app.root.current = "add"
            root.manager.transition.direction = "left"
        
    MDRoundFlatButton:
        text: "Show"
        pos_hint: {"center_x": 0.5, "center_y": 0.6}
        on_press: 
            app.root.current = "show"
        
    MDTextButton:
        text: "Account"
        pos_hint: {"center_x": 0.5, "center_y": 0.1}
        on_press: 
            app.root.current = "settings"
            root.manager.transition.direction = "up"
    
    <AddWindow>:
        name: "add"
        MDRaisedButton:
        text: "BACK"
        md_bg_color: 0, 0, 0, 1
        pos_hint: {"x": 0.01, "y": 0.93}
        on_release: 
            app.root.current = "main"
             root.manager.transition.direction = "right"
                
    MDTextField:
        id: account_link
        hint_text: "Link"
        helper_text: "Insert the Link of the WebSite to enter in the website from 
        this app"
        helper_text_mode: "on_focus"
        line_color_normal: app.theme_cls.accent_color
        pos_hint: {"center_x": 0.5, "center_y": 0.8}
        size_hint_x: None
        width: 1200
    
    MDTextField:
        id: "md_account_name"
        hint_text: "Account"
        helper_text: "Insert the Name of the Account You Want to Save"
        helper_text_mode: "on_focus"
        line_color_normal: app.theme_cls.accent_color
        pos_hint: {"center_x": 0.5, "center_y": 0.7}
        size_hint_x: None
        width: 1200
        
    MDTextField:
        id: "md_account_nickname"
        hint_text: "Nickname"
        helper_text: "Insert the Nickname You Have in the Account"
        helper_text_mode: "on_focus"
        line_color_normal: app.theme_cls.accent_color
        pos_hint: {"center_x": 0.5, "center_y": 0.6}
        size_hint_x: None
        width: 1200
        
    MDTextField:
        id: "md_email"
        hint_text: "Email"
        helper_text: "Insert the Email You Created the Account with"
        helper_text_mode: "on_focus"
        line_color_normal: app.theme_cls.accent_color
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        size_hint_x: None
        width: 1200
        
    MDTextField:
        id: "md_passwd"
        hint_text: "Password"
        helper_text: "Insert Your Password of the Account"
        helper_text_mode: "on_focus"
        line_color_normal: app.theme_cls.accent_color
        pos_hint: {"center_x": 0.5, "center_y": 0.4}
        size_hint_x: None
        width: 1200
        
    MDFillRoundFlatButton:
        text: "Submit"
        pos_hint: {"center_x": 0.5, "center_y": 0.1}
        on_press: app.add_passwd()
    """
    
    class App(MDApp):
    
    def build(self):
        self.title = "Safed" #The Name of the App is "Safed": "Save" + "Saved"
        self.theme_cls.theme_style = "Dark" # Light
        self.theme_cls.primary_palette = "Blue"
        return Builder.load_string(KV)
    
    def add_passwd(self):
        account_link = AddWindow_istance.ids["account_link"].text
        account_name = self.root.ids["md_account_name"].text
        account_nickname = self.root.ids["md_account_nickname"].text
        email = self.root.ids["md_email"].text
        passwd = self.root.ids["md_passwd"].text
    
        #TEST
        print(account_link)
        print(account_name)
        print(account_nickname)
        print(email)
        print(passwd)
    
    
    if __name__ == "__main__":
        App().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多