Kivy Crash Course 9: How to Create a Scrollable Label

Posted by


In this tutorial, we will learn how to create a scrollable label in Kivy. A scrollable label is a widget that allows the user to scroll through a large amount of text that exceeds the size of the label widget. This is useful when you have large blocks of text that you want to display in a limited space.

To create a scrollable label in Kivy, we will use the ScrollView widget along with a Label widget. The ScrollView widget provides the scrolling functionality, while the Label widget is used to display the text.

Let’s get started by creating a new Kivy application and adding a scrollable label to it.

  1. Create a new Python file and import the necessary modules:
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
  1. Create a class for your Kivy application:
class ScrollableLabelApp(App):
    def build(self):
        # Create a ScrollView widget
        scrollview = ScrollView()

        # Create a Label widget with a large amount of text
        label = Label(text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.")

        # Add the Label widget to the ScrollView widget
        scrollview.add_widget(label)

        return scrollview
  1. Run the application:
if __name__ == '__main__':
    ScrollableLabelApp().run()

Now, when you run the application, you should see a scrollable label with the text displayed in it. You can scroll through the text by dragging the scroll bar or using touch gestures on a touch-enabled device.

You can customize the appearance and behavior of the scrollable label by adjusting the properties of the ScrollView and Label widgets. For example, you can change the font size, color, alignment, and other text properties of the Label widget. You can also customize the scroll bar style and behavior of the ScrollView widget.

In this tutorial, we learned how to create a scrollable label in Kivy using the ScrollView and Label widgets. We also discussed how to customize the appearance and behavior of the scrollable label. Experiment with different settings to create a scrollable label that fits your application’s requirements.

0 0 votes
Article Rating

Leave a Reply

24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@petelogiudice8202
17 days ago

Still relevant today to anyone wanting to learn kivy!

@2Sor2Fig
17 days ago

As a fellow Kivy lover, I really appreciate your tutorials. I actually came here from a blog post of yours on a similar topic (trying to design some forms and there were labels involved). Just came here to to leave a view and a like.

@abdnasser2280
17 days ago

so helful bro

@robertsynofzik7519
17 days ago

while i really like your videos, i have to stay the documentation is not to understand for beginners.

@isaacnewton4773
17 days ago

Is it possible for someone to make a Scrolling tutorial/ help that is actually useful, this only explains how to do it with one label, if you try to add multiple labels it tells yous you can only use one child. thus this was a waste of time. we need someone who can explain how to use it say on a different screen, or with other classes and more than one children with it

@GeinponemYT
17 days ago

Great video, but I get a Recursion Error when resizing the window when using a trackpad on my laptop. This is, I think, because it counts me using my trackpad as scrolling (I can see that during movement), and when I resize during scrolling I get this error.
Apparently this can by circumvented by either temporarily disabling the scrolling during resize, or, perhaps a better solution: to override the internal '_update_effect_y'. But both seem like quite hacky ways of doing it.
What do you advise for fixing this problem?

@mwhathaway
17 days ago

Very helpful. Thank you! Nice to see someone else also uses Emacs for python development…

@user-fe1pt7cx6p
17 days ago

So if i have a screen with multiple buttons(Levels buttons) that each one takes me to a certain screen(a specific level)
can i somehow use the scroll widget to be able to scroll down all those levels buttons. because if I have 30 or more of these then in one screen they will be fairly small. So I just want to scroll and show just a few of them every time in my screen

@dragonmanz123
17 days ago

is there a way to write warp parameters in python language instead of kivy

@Reaversword3D
17 days ago

Seriusly?, a "ScrollView" than only accepts one children?. Then, is just for "long-text-in-single-label"?
How can we use it to scroll several entries, as a database, for example?.Must we put all entries in a layout, make the layout be autoadapted at their children's size and then, make the scrollview the parent of that layout?.
How can we do a parent adapts at the sum of their childrens?, knowing the size of the childrens and the number of them at build time?

@sids3194
17 days ago

Thank you soo much! Any idea how to make it autoscrollable so that the viewport always is at newer texts?

@anashasan4469
17 days ago

why no one explains how this shit goes in python language not kivy shit

@MeLeRyZ
17 days ago

Awesome tutorials, thanks a lot!

@user-dr5hq7fw3m
17 days ago

from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView

from kivy.properties import StringProperty

from kivy.base import runTouchApp
from kivy.lang import Builder

Builder.load_string('''
<Scrollabellabel>:
text: str('Kevin,Steven,Mayven,,,'* 100)
label:
text: root.text
font_size: 50
text_size: self.width, None
size_hint_y: None
height: size.texture_size[1]

''')

class Scrollabellabel(ScrollView):
text = StringProperty('')

runTouchApp(Scrollabellabel())

this is my code..which it not working
my python version 2.7

@0xsuperman
17 days ago

Thanks for the great demo, though I am very new in Kivy and am just trying to incoporate your kivy scrollable code into the following simple kivy app that shows what you type after pressing the button; I want the display to become scrollable once the text is long enough. Please give me some tips of how I can implement that into my code (I have tried many things but still not working)

class SomeApp(App):
def build(self):
grid = GridLayout(cols=1, size_hint_x=None, width="600dp")

self.lbl0 = Label(text='Tap and type a word/phrase below') # create a label instance
grid.add_widget(self.lbl0) # physically add the label onto the layout

self.txt1 = TextInput(text='', multiline=False) # create a text input instance
grid.add_widget(self.txt1) # physically add the text input onto the layout

self.lbl1 = Label(text='Display') # create a label instance
grid.add_widget(self.lbl1) # physically add the label onto the layout

btn1 = Button(text='Press') # create a button instance
btn1.bind(on_press=self.mirror) # binding the button with the function below
grid.add_widget(btn1)

return grid

def mirror(self, userInput):
self.lbl1.text = self.txt1.text

SomeApp().run()

@Asdayasman
17 days ago

I'm continuing my tirade of "LOL FUCK PROPERTIES", and have done away with them again here. I also wanna put as much interface stuff in .kv and as much business in .py as possible. At what point will I actually need to use properties?

ex9.py:

import kivy.app

class Ex9App(kivy.app.App):
def build(self):
for item in self.root.walk():
self.__dict__.update(item.ids)

def on_button_click(self):
self.scrolled.text = "this happens"

Ex9App().run()

ex9.kv:

BoxLayout:
ScrollLabel:
id: scrolled
Button:
on_press: app.on_button_click()

<ScrollLabel@ScrollView>:
text: "I will defend my love with my life, and my love of it, come hell or high water, plague, drought, or famine. We promised "'til death do", and today, none other but death may."
orientation: "horizontal"
Label:
text: root.text
size_hint_x: None
width: self.texture_size[0]

@DoubleAAmazin3
17 days ago

@5:14 you display annotation that says "If you make the text too long you will exceed the gpu texture size and only see a black screen!  If you have that problem, try" ….and that is all it says.  What exactly do you do when this happens?  Because this is the exact problem I am encountering, for I am trying to display lots and lots of text.

@edvinbeqari2318
17 days ago

Could you make a "scrollable gridlayout tutorial," please? Thank you for your work. Very helpful.

@MarkiePQ
17 days ago

this turns to a BLACK label and idk if its a bug but whenever you append some multi-line strings (something around 50 lines) the background of Label turns to Black, any fix for this?

@matthewblott
17 days ago

Is it pronounced "kiv e" or "keevy"?

24
0
Would love your thoughts, please comment.x
()
x