Shreyansh Dash
Shreyansh Dash

Reputation: 177

How to in kivy make labels go to the next line if the window size is not big enough

'''

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class MyGrid(GridLayout):
    def __init__(self, **kwargs):# handle as many can com in **kwargs
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.add_widget(Label(text="Super long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper long text"))
class MyApp(App):
    def build(self):
        return MyGrid()

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

'''

Up above is my code and when I run the code the text is out of the window: enter image description here So Is there any way I can move the text to the next line if it crosses the windows geometry.

Any suggestions will help thank you.

Upvotes: 2

Views: 692

Answers (2)

sdfdfs
sdfdfs

Reputation: 11

You can't it's not possible in kivy. Though its possible in tkinter

Upvotes: 1

John Anderson
John Anderson

Reputation: 39092

You can use the text_size attribute, like this:

self.add_widget(Label(text_size=(500, None), text="Super long textSuper ...

Another way is to use kivy language to set up bindings that adjust the text_size as the window size changes:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout

kv = '''
<MyLabel>:
    size_hint: 0.5, 0.5
    text_size: self.size
'''

class MyLabel(Label):
    pass


class MyGrid(GridLayout):
    def __init__(self, **kwargs):# handle as many can com in **kwargs
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.add_widget(MyLabel(text="Super long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper long text"))


class MyApp(App):
    def build(self):
        Builder.load_string(kv)
        return MyGrid()

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

This code sets text_size to always follow the widget size in the MyLabel class. Using kv sets up a binding so that the text_size will change as the widget size changes.

Upvotes: 1

Related Questions