Search code examples
pythonoopscrollkivygrid-layout

kivy - First row in a GridLayout is blank and the last one is hidden, ScrollView


I am trying to make a long list scrollable in Kivy (like a list of songs). Everything works fine except that it looks like everything is shifted one spot down (I tried with printing the position and the blank one(first row) would have the value the first row, but would output a blank line,then the second row would also have the correct value but would output to the screen the value of the first row, I described it very poorly so if anyone needs more information I will gladly post the code)

It does not produce an error but it is unpleasant and it makes the rest of code not work.

Here is the smallest bit of code I could make to reproduce the error: I did not use .kv file for the sake of simplifying, also note that variables: brojPjesmi is a variable for how many rows to make var1 and var2 are usually variables that are printed to the row

from kivy.app import *
from kivy.uix.label import *
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout


brojPjesmi = 20
var1 = 0
var2 = 1
class MyRow(ScrollView):
    def __init__(self, **kwargs):
        super(MyRow, self).__init__(**kwargs)
        self.size_hint=(1,None)
        self.size=(Window.width,Window.height)
        layout = GridLayout(cols = 1,spacing= 10,size_hint_y = None)
        layout.bind(minimum_height = layout.setter('height'))
        for i in range(brojPjesmi):
            self.z = OneRow()
            self.u = self.z.make(var1,var2)
            layout.add_widget(self.u)
        self.add_widget(layout)

class OneRow(GridLayout):
    def make(self,x,y):
        self.cols = 3
        self.rows = 1
        self.height = 25
        self.size_hint_y = None
        self.Treci = Label(text = str(""),size_hint_y= None,size_hint_x =.1)
        self.prviDio = Label(text = str("y"),size_hint_y= None)
        self.drugiDio = Label(text = str("x"),size_hint_y= None)
        self.add_widget(self.Treci)
        self.add_widget(self.prviDio)
        self.add_widget(self.drugiDio)
        return self
class langApp(App):
    def build(self):
        return MyRow()

if __name__ == '__main__':
    langApp().run()

Update: I think the error is somewhere within these 3 lines of code:

            self.z = OneRow()
            self.u = self.z.make(var1,var2)
            layout.add_widget(self.u)

Because when I replaces them with

    btn = Button(text=str(i), size_hint_y=None, height=40)
    layout.add_widget(btn)

It worked flawlessly


Solution

  • I believe the problem is with the size of the Labels created in the make() method. Try something like:

        self.Treci = Label(text = str(""), size_hint=(None, None), height=25, width=50)
        self.prviDio = Label(text = str("y"), size_hint=(None, None), height=25, width=50)
        self.drugiDio = Label(text = str(x), size_hint=(None, None), height=25, width=50)