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'
Note that the documentation says:
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 theAddWindow
rule (not 100% sure due to the indentation of yourkv
snippet). If that is the case, then you need a reference to the instance ofAddWindow
that appears in your GUI: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:Note that this also requires a couple corrections to your
kv
. Wherever you have anything like:it should be changed to:
One example is
id: "md_account_name"
.