I am trying to have a screen which contains a StackLayout. This StackLayout contains several labels of short to long length. The whole content of the StackLayout should also be scrollable with ScrollView. I tried the setup below, but basically every Label is out of place. I have tried several alternative setups based on my understanding of kivy documentation and of (somewhat) similar questions on the internet, but non of them really worked.
Here is a minimal working version of the code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
Window.size = (350, 600)
class RootWindow(Screen):
def __init__(self, **kwargs):
super(RootWindow, self).__init__(**kwargs)
self.searchButton = Button(text="Search",
size_hint=(1,0.8))
self.searchButton.bind(on_press=self.search)
self.add_widget(self.searchButton)
# some irrelevant code
def search(self,instance):
#here some omitted code which grabs some strings from a SQLite db and saves results in src, ingsList and steps
src = 'Lorem ipsum'
ingsList = ['Lorem', 'ipsum', 'dolor', 'sit amet']
WMan.transition.direction = 'left'
WMan.current = 'second'
# theses should be aligned at center of the screen
WMan.current_screen.title.add_widget(Label(text=src,
size_hint=(1,None)))
WMan.current_screen.ingredients.add_widget(Label(text="Ingredients:",
text_size=(self.width, None),
size_hint=(1,None),
height='30sp'))
# these should be left aligned
for i in range(0,len(ingsList)):
WMan.current_screen.ingredients.add_widget(Label(text=ingsList[i],
text_size=(self.width, None),
size_hint=(0.9,None),
height='20sp'))
# center aligned
WMan.current_screen.ingredients.add_widget(Label(text="Steps:",
text_size=(self.width, None),
size_hint=(1,None),
height='30sp'))
# this should be left aligned (or, ideally, justified)
steps = "Duis finibus risus tempor nisl scelerisque, quis facilisis augue pretium. Nullam sit amet nibh ex. Pellentesque lobortis eget ipsum a congue. Nunc luctus odio sit amet arcu interdum, id pharetra urna semper. Proin at turpis vel neque facilisis pretium ut sed massa. Phasellus elit diam, elementum at tempus non, eleifend quis libero. Integer convallis tortor eget mattis eleifend."
WMan.current_screen.ingredients.add_widget(Label(text=steps,
text_size=(self.width, None),
size_hint=(1,None),
pos_hint=(None,None),
height='10sp'
))
class SecondWindow(Screen):
def __init__(self, **kwargs):
super(SecondWindow, self).__init__(**kwargs)
self.title = StackLayout(pos_hint={"x":0.05, "top":0.9},
size_hint = (0.9,0.2))
self.add_widget(self.title)
self.ingredients = StackLayout(pos_hint={"x":0.05, "top": 0.8},
size_hint=(0.9,None),
size_hint_y=None,
orientation='lr-tb',
spacing = (0,2))
self.ingredients.bind(minimum_height = self.ingredients.setter('height'))
self.scroll = ScrollView(size_hint=(1,None),
size=(self.width, Window.height),
pos_hint={"x": 0.05, "top":0.8})
self.scroll.add_widget(self.ingredients)
self.add_widget(self.scroll)
WMan = ScreenManager()
WMan.add_widget(RootWindow(name='root'))
WMan.add_widget(SecondWindow(name='second'))
class MyApp(App):
def build(self):
return WMan
if __name__ == "__main__":
MyApp().run()