Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping the text of a Kivy Label

Tags:

python

kivy

So I am writting a program using the Kivy GUI and I really don't want to use a .kv file but write it all in the python file. The problem is that inside the MainScreen Class i want to add some Labels but i cannot make their text to wrap according to window size changes. When i try self.size I only get 100x100. I have tried all the suggestions from the Kivy tutorial book but nothing worked. I know i could just make the text multiline using \n or set a standard Label size, but that wouldn't be a real solution. I need the Label size and text to follow the whole window changes and the text to be wrapped.

I have simplified the program to focus just on this issue.

Here's the code:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Rectangle, Color


class MainScreen(FloatLayout):
    """MAIN WINDOW CLASS"""

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

        with self.canvas.before:
            Color(0.988, 0.725, 0.074, 1, mode='rgba')
            self.rect = Rectangle(pos=self.pos, size=self.size)
        self.bind(size=self.update_rect)


        #TITLE LABEL
        self.add_widget(Label(text="A very Long Sentence that needs to be wrapped.",
                              bold = True,
                              font_size="20sp",
                              pos_hint={'center_x': 0.5, 'center_y': .85},
                              size_hint=(None, None),
                              halign="center",
                              color=(0.055, 0.235, 0.541, 1)))

    def update_rect(self, *args):
        """FUNCTION TO UPDATE THE RECATANGLE OF CANVAS TO FIT THE WHOLE SCREEN OF MAINSCREEN ALWAYS"""
        self.rect.pos = self.pos
        self.rect.size = self.size

class main(App):
    """BUILDING THE APP"""
    def build(self):
        return MainScreen()

if __name__ == "__main__":
    main().run()

Thank you.

like image 266
George Bou Avatar asked Apr 27 '17 19:04

George Bou


People also ask

How do I make KIVY label multiline text?

You can easily make the text multiline using '\n' at the end of each line.

How do you wrap text in a label in Visual Studio?

Just set Label AutoSize property to False. Then the text will be wrapped and you can re-size the control manually to show the text. Save this answer.

What is wrapper text?

"Wrapping text" means displaying the cell contents on multiple lines, rather than one long line. This will allow you to avoid the "truncated column" effect, make the text easier to read and better fit for printing.


2 Answers

First, let me tell you that you are going to make your life way harder than it should, kv is not hard to learn, and it allows kivy to do a lot more of the heavy lifting for you.

in kv, doing something like

Label:
    text: 'blah blah ' * 1000
    text_size: self.width, None
    size_hint: 1, None
    height: self.texture_size[1]

would usually do the trick, since the widget is using the whole width of its container, and the text_size being bound to its width, the texture would be recomputed everytime this width changes, and the height of the widget would automatically fit to the height of the texture.

Now, to do the same without kv.

  label = Label(text='blah blah '* 1000, size_hint=(1, None))
  label.bind(
      width=lambda *x: label.setter('text_size')(label, (label.width, None),
      texture_size=lambda *x: label.setter('height')(label, label.texture_size[1]))

that is, you need to explicitly tell kivy that it has to react to changes in the various properties of the label, and recompute the values of other properties, while with kv, it automatically detects that some values depend on others, and will know it needs to bind on them on its own.

setter is a utility function which allow you to avoid defining setter functions in simple cases, if not for it, here, you would have to define a function for each of the binds, and do something like "self.text_size = self.width, None" in them, because lambda doesn't allow assignation.

One of the reasons kv exists, is that it allows to process ourselves the expressions in your code, and detect the dependencies, if you do it all in python, there is no way kivy can detect such dependencies, because python itself doesn't care about them, and doesn't tell kivy about them.

Now if you want to tell us why you want to avoid kv, please do, but i'm pretty sure these reasons come from a lack of knowing kv rather than a real problem with it.

like image 55
Tshirtman Avatar answered Oct 26 '22 21:10

Tshirtman


I would make use of the wonderful OOP.

You can just create class like this:

class WrappedLabel(Label):
    # Based on Tshirtman's answer
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.bind(
            width=lambda *x:
            self.setter('text_size')(self, (self.width, None)),
            texture_size=lambda *x: self.setter('height')(self, self.texture_size[1]))

Then use your class in any part of your code you want "the wrapping behavior". Example:

my_label = WrappedLabel(text="A very L" + "o"*200 + "ng Sentence that needs to be wrapped.",
                        bold=True,
                        font_size="20sp")
like image 21
Ferd Avatar answered Oct 26 '22 21:10

Ferd