how cen I make these dynamically added widgets appear from the bottom of the scrollview instead of the top. Kind of like WhatssApp or Telegram or Messenger. I am a bit stuck on this one. I tried with PushMatrix and PopMatrix, but then the text appeared upside down.
py file:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
Builder.load_file('scrollview_modification.kv')
class MyLayout(Widget):
def add_widgets(self):
settings_label = SettingsLabel(text="Widget added")
self.ids.widgets.add_widget(settings_label)
class SettingsLabel(Label):
pass
class AwesomeApp(App):
def build(self):
Window.clearcolor = (0,1,1,1)
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
kv file:
<MyLayout>
BoxLayout:
orientation:'vertical'
size: root.width, root.height
size_hint: 1, 0.6
Button:
text: "add_widgets"
on_press: root.add_widgets()
size_hint: 1, 0.25
ScrollView:
do_scroll_x: False
do_scroll_y: True
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
id: widgets
spacing: 3
padding: 3
<SettingsLabel>
markup: True
bold: True
size_hint: 1, None
height: 50
font_size: 16
col: (179/255, 89/255, 0 , 1)
canvas.before:
Color:
rgba: self.col
RoundedRectangle:
size: self.size
pos: self.pos
radius: [10]
To make the dynamically added widgets appear from the bottom of the ScrollView, you can set the height of the widgets BoxLayout to be the sum of the heights of all the widgets it contains. This way, when a new widget is added, it will be placed at the bottom of the ScrollView.
You can modify your kv file like this:
<MyLayout>
BoxLayout:
orientation:'vertical'
size: root.width, root.height
size_hint: 1, 0.6
Button:
text: "add_widgets"
on_press: root.add_widgets()
size_hint: 1, 0.25
ScrollView:
do_scroll_x: False
do_scroll_y: True
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
id: widgets
spacing: 3
padding: 3
and the python script as
class MyLayout(Widget):
def add_widgets(self):
settings_label = SettingsLabel(text="Widget added")
self.ids.widgets.add_widget(settings_label)
self.ids.scroll_to(settings_label)
self.ids.widgets.height = sum(child.height for child in self.ids.widgets.children)
This will make the new widget appear at the bottom of the ScrollView. Note that if the ScrollView is not full, the new widget may appear near the top, but as more widgets are added, they will appear at the bottom.