title: Kivy Update Widget
body: <p>I read in a text file called data.txt containing the usernames and passwords. The format of the text file is:</p>
  
  <p>username1<br>
  password1<br>
  username2<br>
  password2<br>
  username3<br>
  username3<br>
  etc.</p>
  
  <p>I read in the usernames and make a corresponding button for each username. When I first start the program, it shows everything in data.txt and works. However, when I add a user using the program, it does not update the list of users on the main screen. How would I update the list?</p>
  
  <pre><code>import kivy
  from kivy.app import App
  from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
  from kivy.uix.boxlayout import BoxLayout
  from kivy.uix.floatlayout import FloatLayout
  from kivy.uix.label import Label
  from kivy.uix.button import Button
  from kivy.core.window import Window
  from kivy.config import Config
  from kivy.properties import ObjectProperty
  from kivy.uix.textinput import TextInput
  from kivy.uix.popup import Popup
  from numpy import loadtxt
  from kivy.uix.widget import Widget
  from os.path import sep, expanduser, isdir, dirname
  from kivy.uix.stacklayout import StackLayout
  from functools import partial
  from kivy.uix.scrollview import ScrollView
  from kivy.uix.gridlayout import GridLayout
  
  class AddUserWindow(Screen):
      def __init__(self,**kwargs):
          super (AddUserWindow,self).__init__(**kwargs)
          addNewUserLayout = FloatLayout()
          newUserLabel = Label(text="User Name:", font_size='15dp', size_hint = (.5, .05), pos = (500, 450), halign = 'right')
          passwordLabel = Label(text="Password:", font_size='15dp', size_hint = (.5, .05), pos = (500, 400), halign = 'right')
          confirmPasswordLabel = Label(text="Confirm Password:", font_size='15dp', size_hint = (.5, .05), pos = (500, 350), halign = 'right')
          self.newUserInput = TextInput(text = "", size_hint = (.25, .05), pos = (875, 450), font_size='15dp', multiline = False, write_tab = False)
          self.passwordInput = TextInput(text = "", password = True, size_hint = (.25, .05), pos = (875, 400), font_size='15dp', multiline = False, write_tab = False)
          self.confirmPasswordInput = TextInput(text = "", password = True, size_hint = (.25, .05), pos = (875, 350), font_size='15dp', multiline = False, write_tab = False)
          cancelButton = Button(text="Cancel",size_hint = (.2, .05), pos =  (650,0))
          cancelButton.bind(on_press = self.changeToMainWindow)
          okButton = Button(text="Ok",size_hint = (.2, .05), pos =  (350,0))
          okButton.bind(on_press=self.newUserCheck)
          addNewUserLayout.add_widget(cancelButton)
          addNewUserLayout.add_widget(okButton)
          addNewUserLayout.add_widget(newUserLabel)
          addNewUserLayout.add_widget(passwordLabel)
          addNewUserLayout.add_widget(confirmPasswordLabel)
          addNewUserLayout.add_widget(self.newUserInput)
          addNewUserLayout.add_widget(self.passwordInput)
          addNewUserLayout.add_widget(self.confirmPasswordInput)
  
          self.add_widget(addNewUserLayout)
  
      def newUserCheck(self, *args):
  
          if self.newUserInput.text == "":
              content = Button(text = 'Close')
              popup = Popup(title = 'Please enter user name', size_hint = (.4, .15), content = content, auto_dismiss = False)
              content.bind(on_press = popup.dismiss)
              popup.open()
              return
  
          if self.passwordInput.text == "":
              content = Button(text = 'Close')
              popup = Popup(title = 'Please enter password', size_hint = (.4, .15), content = content, auto_dismiss = False)
              content.bind(on_press = popup.dismiss)
              popup.open()
              return
  
          if self.confirmPasswordInput.text == "":
              content = Button(text = 'Close')
              popup = Popup(title = 'Please confirm password', size_hint = (.4, .15), content = content, auto_dismiss = False)
              content.bind(on_press = popup.dismiss)
              popup.open()
              return
  
          if self.passwordInput.text != self.confirmPasswordInput.text:
              content = Button(text = 'Close')
              popup = Popup(title = 'Passwords must match', size_hint = (.4, .15), content = content, auto_dismiss = False)
              content.bind(on_press = popup.dismiss)
              popup.open()
              return
  
  
          file = open('data.txt', 'r')
          initial = file.readlines()
          databaselines = sum(1 for line in open('data.txt'))
          file.close()
  
  
          users = databaselines / 2
          data = [[0 for x in range(2)] for y in range(users)]
          currLine = 0
          range(users)
          for i in range(users):
              data[i][0] = initial[currLine].rstrip()
              data[i][1] = initial[currLine + 1].rstrip()
              currLine += 2
  
          inDatabase = 0
          for i in range(users):
              if data[i][0] == self.newUserInput.text:
                  inDatabase = 1
                  break
          if inDatabase == 1:
              content = Button(text = 'Close')
              popup = Popup(title = 'Already in database', size_hint = (.4, .15), content = content, auto_dismiss = False)
              content.bind(on_press = popup.dismiss)
              popup.open()
              return
  
          with open("data.txt", "a") as dataFile:
              dataFile.write(self.newUserInput.text + "\n")
              dataFile.write(self.passwordInput.text + "\n")
          self.changeToMainWindow
  
  
      def changeToMainWindow(self, *args):
          self.newUserInput.text = ""
          self.passwordInput.text = ""
          self.confirmPasswordInput.text = ""
          self.manager.current = 'mainScreen'
  
  class MainWindow(Screen):
  
      def __init__(self,**kwargs):
          super (MainWindow,self).__init__(**kwargs)
  
          self.mainLayout = FloatLayout()
  
          logoutButton = Button(text="Logout",size_hint = (.2, .1), pos =  (650,0))
          logoutButton.bind(on_press = exit)
          addNewUserButton = Button(text="New User",size_hint = (.2, .1), pos =  (350,0))
          addNewUserButton.bind(on_press=self.changeToAddUser)
          self.mainLayout.add_widget(logoutButton)
          self.mainLayout.add_widget(addNewUserButton)
          file = open('data.txt', 'r')
          initial = file.readlines()
          databaselines = sum(1 for line in open('data.txt'))
          file.close()
  
  
          users = databaselines / 2
          data = [[0 for x in range(2)] for y in range(users)]
          currLine = 0
          range(users)
          for i in range(users):
              data[i][0] = initial[currLine].rstrip()
              data[i][1] = initial[currLine + 1].rstrip()
              currLine += 2
  
          self.layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
          self.layout.bind(minimum_height=self.layout.setter('height'))
          for i in range(users):
              self.btn = Button(text=data[i][0], size_hint_y=None, height=40)
              self.layout.add_widget(self.btn)
          self.root = ScrollView(size_hint=(.45, None), pos = (600, 100), size=(Window.width, Window.height))
          self.root.add_widget(self.layout)
  
          self.mainLayout.add_widget(self.root)
          self.add_widget(self.mainLayout)
  
  
  
  
      def changeToAddUser(self, *args):
          self.manager.current = 'addUserScreen'
  
  
  
  class TestApp(App):
          def build(self):
              self.my_screenmanager = ScreenManager(transition = NoTransition())
              self.mainScreen = MainWindow(name='mainScreen')
              self.addUserScreen = AddUserWindow(name = 'addUserScreen')
              self.my_screenmanager.add_widget(self.mainScreen)
              self.my_screenmanager.add_widget(self.addUserScreen)
  
              Window.size = (1200, 800)
              return self.my_screenmanager
  
  if __name__ == '__main__':
      TestApp().run()
  </code></pre>
  
  <p>Thanks so much in advance!!</p>