Skip to content
Advertisement

Kivy: Labels not being displayed in python file with add_widget()

I’m quite new to Kivy and have been repeatedly having issues with displaying things. I am building a basic app to track students, assign them to school houses (like in Harry Potter) and display them. I created a “for” loop to add users to labels in a stack layout which would then display them in a scroll view, but for some reason, when I run the file, nothing is displayed. I have no idea what is going on. Please run the file yourself, to troubleshoot the problem

Note: The problem concerns lines 261-280 in the python file

Python file:

JavaScript

Kivy file:

JavaScript

Fake student credentials text file (put it into a text file called “passwordStudents.txt” in a folder called “passwords”):

JavaScript

Student houses (create four text files with the students’ first and last names at random called “house1.txt”, “house2.txt”, etc. in a folder called “houses”)

Advertisement

Answer

A couple problems with your code:

  1. Everywhere you use code like StudentsGridList().foo('4'), you are creating a new instance of StudentsGridList and calling the foo() method of that new instance. Doing so will have no effect on the StudentsGridList instance that is in the StudentSpecificHomeScreen of your GUI.
  2. The if statement if int(specific_house) == int: in your foo() method will always be False, since your are comparing an integer to a built-in method.

To correct the first problem, you will probably need to use the get_screen() method of ScreenManager along with some ids in order to access the instance of StudentsGridList that is actually in your GUI. You will probably need to assign an id to the StudentsGridList. This can be done in your kv file:

JavaScript

Then, in your code you can replace:

JavaScript

with:

JavaScript

And similarly for the other occurrences of StudentsGridList().foo().

As for the second problem, I am not sure of the purpose of that if statement. Perhaps that if statement should just be removed.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement