0

After I collect the user input in kivy, I want to use it to update all my screens(arranged in classes). The issue is that when I collect the username from the first screen, I am finding it difficult to use the username to make updates on the other screens. For example, the second screen contains a label that says hello, username. However, after switching to the second screen, it retains its default value. What I have tried:

  • I tried creating a function in the second window and using the return value(which contains the username) as the text for the label. To do so I had to initialize it at first. Unfortunately, it seems that despite I call the function when I switch to the next screen(by pressing a button), the label has already been initialized and would not change its value.
  • I tried looking for an on_load function in kivy to update it when the window loads, but I couldn't find any.

    Code

class MainWindow (Screen):

    '''This is the mainwindow class where I collect the user input'''

    password = ObjectProperty(None)
    username = ObjectProperty(None)
    active_user = ObjectProperty(None)
    def check(self):
        if self.password.text in Student.LIST_OF_STUDENTS.keys():
            self.active_user = Student.LIST_OF_STUDENTS[self.password.text]

            #After collecting user input I tried setting the username to SeconWindow's attribute (user)
            #So when I click the submit button and it verifies, it would change the username in the second window

            SecondWindow.user = self.active_user.club
            return True

class SecondWindow (Screen):

    '''THis is the Second window and its property user'''
    user = Student.LIST_OF_STUDENTS['123'].club

This is the Kv file, linking the second window

<SecondWindow>

    name: 'second'
    FloatLayout:
        Label
            text: root.user
            pos_hint: {'y':0.9, 'x':0.1}
            text_size: self.size
            font_size: 30

However, when It switches to the second window, the label just retains its original user's name rather than switching to the 'new' one. Even though I changed it with the submit button in the first class.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Smart
  • 1
  • 2
  • Show what you tried – eyllanesc Oct 26 '19 at 18:30
  • Please how do I post my code? – Smart Oct 26 '19 at 20:11
  • edit your question and add your code as text – eyllanesc Oct 26 '19 at 20:12
  • http://inclem.net/2019/06/20/kivy/widget_interactions_between_python_and_kv/ contains some relevant information – inclement Oct 27 '19 at 11:33
  • You probably need to change the `user` in `SecondWindow` into an actual property like: `user = SringProperty(Student.LIST_OF_STUDENTS['123'].club)`. – John Anderson Oct 27 '19 at 14:52
  • To inclement: Thank you for the link, I found it very insightful. However, The issue I have is not accessing the label, but **updating** the label. From my code, the label on initialization assumes as value, however, after the user signs in, it is expected to change the value to match the user's club name. Thanks for the understanding! – Smart Oct 27 '19 at 17:21
  • This forum( https://stackoverflow.com/questions/26656164/how-to-change-text-of-a-label-in-the-kivy-language-with-python )answers my question perfectly, however, the label changes by pressing a button. Can the function be called when the second screen comes up? – Smart Oct 27 '19 at 17:29

0 Answers0