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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…