Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

First Python/Kivy/KivyMD App. Unable to save data from textfield to variable + data binding to MDList

I recently started to work on a school turn in app in python/kivy. I'm at the state where I created and UI in kivymd and a user class. I want to save the info into a list of user objects but I get a KeyError: 'firstname' error message. Do you guys know what can be the problem?

Also what I want to implement, is that I put a MDList to the second screen. With Data Binding I want to show the First and Last names of the users that are in the list. Any idea how can I achive that? I didn't find and usable solutions on the web.

Thank you for your help in advance!

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import _textinput_list

screen_helper = """
ScreenManager:
    id: smn
    AddUserScreen:
    ViewListScreen:
    ModifyUser:
    
<AddUserScreen>:
    id: screen1
    name: 'adduser'
    MDTextField:
        id:firstname
        hint_text: "First Name"
        helper_text: "Please enter your firts name"
        helper_text_mode: "on_focus"
        pos_hint: {'center_x': 0.5, 'center_y': 0.9}
        size_hint_x: None
        width:300
    MDTextField:
        id: lastname
        hint_text: "Last Name"
        helper_text: "Please enter your last name"
        helper_text_mode: "on_focus"
        pos_hint: {'center_x': 0.5, 'center_y': 0.8}
        size_hint_x: None
        width:300
    MDTextField:
        id: city
        hint_text: "City of Residence"
        helper_text: "Please enter your city of residence"
        helper_text_mode: "on_focus"
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        size_hint_x: None
        width:300 
    MDTextField:
        id: year
        hint_text: "Birthyear"
        helper_text: "Please enter your birthyear"
        helper_text_mode: "on_focus"
        pos_hint: {'center_x': 0.5, 'center_y': 0.6}
        size_hint_x: None
        width:300               
    MDRectangleFlatButton:
        text: 'View User List'
        pos_hint: {'center_x': 0.7, 'center_y': 0.1}
        on_press: root.manager.current = 'viewlists'        
    MDRectangleFlatButton:
        text: 'Add User'
        pos_hint: {'center_x': 0.3, 'center_y': 0.1}
        on_press: root.addUser()
        
        
<ViewListScreen>:
    id: screen2
    name: 'viewlists'
    MDList:
        id: container
        pos_hint: {'center_x': 0.5, 'center_y': 0.9}
    MDRectangleFlatButton:
        text: 'Add New User'
        pos_hint: {'center_x': 0.3, 'center_y': 0.1}
        on_press: root.manager.current = 'adduser'       
    MDRectangleFlatButton:
        text: 'Modify User'
        pos_hint: {'center_x': 0.7, 'center_y': 0.1}
        on_press: root.manager.current = 'modify'

<ModifyUser>:
    id: 'screen3"
    name: 'modify'
    MDRectangleFlatButton:
        text: 'Add New User'
        pos_hint: {'center_x': 0.3, 'center_y': 0.1}
        on_press: root.manager.current = 'adduser'       
    MDRectangleFlatButton:
        text: 'View User'
        pos_hint: {'center_x': 0.7, 'center_y': 0.1}
        on_press: root.manager.current = 'viewlists'
        
"""


class AddUserScreen(Screen):
    def addUser(self):
        screen1_instance = sm.get_screen('adduser')
        firstname1 = screen1_instance.ids['firstname'].text
        lastname1 = screen1_instance.ids['lastname'].text
        city1 = screen1_instance.ids['city'].text
        year1 = screen1_instance.ids['year'].text
        userlist.append(User(firstname1, lastname1, city1, year1))
        print('New User Added')


class ViewListScreen(Screen):
    pass


class ModifyUser(Screen):
    pass


userlist = []
sm = ScreenManager()
sm.add_widget(AddUserScreen(name='adduser'))
sm.add_widget(ViewListScreen(name='viewlists'))
sm.add_widget(ModifyUser(name='modify'))


class App(MDApp):

    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen


App().run()


class User:
    def __init__(self, firstname, lastname, country, year):
        self.firstname = firstname
        self.lastname = lastname
        self.country = country
        self.year = year
question from:https://stackoverflow.com/questions/65649427/first-python-kivy-kivymd-app-unable-to-save-data-from-textfield-to-variable-d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A few issues with your code. First, the lines:

sm = ScreenManager()
sm.add_widget(AddUserScreen(name='adduser'))
sm.add_widget(ViewListScreen(name='viewlists'))
sm.add_widget(ModifyUser(name='modify'))

are building a widget tree that is not used in your App. These lines can be removed.

Second, the User class definition appears after the App().run(), which means that the running App does not have that class available. This class definition should be moved before the App().run()

Third, the addUser() method uses the sm defined above, which is not part of your App widget tree, has not been displayed, and therefore, has no ids (causing the error). A correction to the addUser() method accesses the actual widget tree of the App. Like this:

class AddUserScreen(Screen):
    def addUser(self):
        screen1_instance = self.manager.get_screen('adduser')  # get correct Screen
        firstname1 = screen1_instance.ids['firstname'].text
        lastname1 = screen1_instance.ids['lastname'].text
        city1 = screen1_instance.ids['city'].text
        year1 = screen1_instance.ids['year'].text
        userlist.append(User(firstname1, lastname1, city1, year1))
        print('New User Added')

Also, naming your App as App is probably a bad idea, as App is the name of the base class of MDApp. A better idea is to choose a name that is unique.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...