Step
Step

Reputation: 36

Kivy can't get text input using ScreenManager

I'me working on a project and I'm using kivy.

I want to crete an app and I need multiple pages so I'm using ScreenManages. I also need to take User Input in one of the pages and save it, so I've used MDTextField for take the text and a button to save the data. When i press the button the app should take the data from the text field and save it in a file with sqlite3, but when I press the button it give me a really strange error. I've tried to rewrite only that page of the app without the ScreenManager and it works. How can I make it work also with the ScreenManager ?

(How can I get the User Input using MDTextField and ScreenManager)

I will show you some lines of code to make you understand better:

This is the Kivy Code:

<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

This is the code to take the data from the Text Field (that part of code it's executed when the user presses the submit button):

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

This is the error i get when i press the button:

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

Upvotes: 0

Views: 812

Answers (2)

John Anderson
John Anderson

Reputation: 38947

Note that the documentation says:

ids are added to the root widget’s ids dictionary.

Poorly worded documentation, because they elsewhere refer to "root widget" as the root of the entire GUI. But in this case "root widget" is the root of the rule where the ids are defined. In your case that might be the AddWindow rule (not 100% sure due to the indentation of your kv snippet). If that is the case, then you need a reference to the instance of AddWindow that appears in your GUI:

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

Without seeing more of your code, I can only guess at the appropriate method to access the instance of AddWindow.

With the addition of a complete code, I can now help you. Here is a modified version of your add_passwd() method:

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)

Note that this also requires a couple corrections to your kv. Wherever you have anything like:

id: "some_id"

it should be changed to:

id: some_id

One example is id: "md_account_name".

Upvotes: 1

Step
Step

Reputation: 36

This is More of my code:

# 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()

Upvotes: 0

Related Questions