pinkcat
pinkcat

Reputation: 337

How to create scrollable FloatLayout in python kivy

I am creating a search menu for a list of players' accounts.
The names of the players that are in the database should be displayed in a ScrollView.

I tried to do this with either a GridLayout or a ScrollView but I want to place player's nickname next to his photo, so I think, that using FLoatLayout is a better approach to achieve that.

Still, I can't make FLoatLayout scrollable .

This code is working:

class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='horizontal', pos=(200, 100))
        left=ScrollView(size_hint=[None, None], size=(194, 334))
        leftGrid = GridLayout(cols=1, size_hint_y=None,padding=20)
        leftGrid.bind(minimum_height=leftGrid.setter('height'))
        for x in range (34):
            self.image = Image(
                source='other_nick.gif', pos=(0, 0),height=30,size_hint_y=None,)
            label = Label(
                text='hello world', pos=(-15, 20), height=30,size_hint_y=None,)
            leftGrid.add_widget(self.image)
            leftGrid.add_widget(label)

        left.add_widget(leftGrid)
        root.add_widget(left)

        return root
if __name__ == "__main__":
    TestApp().run()

But not as I want it, since it places the text next to the image but the FloatLayout is not scrollable:

class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='horizontal', pos=(200, 100))
        left=ScrollView(size_hint=[None, None], size=(194, 334))
        fl = FloatLayout(size_hint_y=None)
        y = 50
        for x in range (34):
            self.image = Image(
                source='other_nick.gif', pos(0,y),height=30,size_hint_y=None,)
            label = Label(
                text='hello world', pos=(-15, y), height=30,size_hint_y=None,)
            fl.add_widget(self.image)
            fl.add_widget(label)
            y -= 30

        left.add_widget(fl)
        root.add_widget(left)

        return root
if __name__ == "__main__":
    TestApp().run()

Upvotes: 1

Views: 395

Answers (1)

PalimPalim
PalimPalim

Reputation: 3068

I think your first try was closer than the second. I have encapsulated label and image in a boxlayout. The rest is unchanged.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.image import Image
from kivy.uix.label import Label

class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='horizontal', pos=(200, 100))
        left=ScrollView(size_hint=[None, None], size=(194, 334))
        leftGrid = GridLayout(cols=1, size_hint_y=None,padding=20)
        leftGrid.bind(minimum_height=leftGrid.setter('height'))
        for x in range (34):
            image = Image(
                source='other_nick.gif', pos=(0, 0),height=30,size_hint_y=None,)
            label = Label(
                text='hello w. ' + str(x), pos=(-15, 20), height=30,size_hint_y=None,)
            box = BoxLayout(orientation="horizontal", height=40, size_hint_y=None)
            box.add_widget(label)
            box.add_widget(image)

            leftGrid.add_widget(box)

        left.add_widget(leftGrid)
        root.add_widget(left)

        return root
if __name__ == "__main__":
    TestApp().run()

Upvotes: 1

Related Questions