Align Label

64 views
Skip to first unread message

WS

unread,
May 15, 2024, 5:16:28 PM5/15/24
to Kivy users support

Hello everyone, I can justify the left (text_align='left') , the center (text_align='center'), but I can't justify the right (text_align='right'). Why is the limit - 1778?


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

class TitleLineWidget(Label):
def __init__(self, title, text_align='center', **kwargs):
super(TitleLineWidget, self).__init__(**kwargs)
self.text = title
self.text_align = text_align
self.bind(size=self.update_line)

with self.canvas.before:
Color(0.2, 0.7, 0.3, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)

def update_line(self, instance, value):
if self.text_align == 'left':
self.text_size = (self.width - 40, self.height - 810)
self.text_pos = (self.x, self.center_y)
elif self.text_align == 'right':
self.text_size = (self.width - 1778, self.height - 810)
self.text_pos = (self.x, self.center_y)

class TitleLineApp(App):
def build(self):
title_line_widget = TitleLineWidget(title="text", text_align='right')
return title_line_widget

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






ElliotG

unread,
May 15, 2024, 7:49:01 PM5/15/24
to Kivy users support
To set the alignment of text within a Label you must set the text_size.  The text_size specifies the bounding box for the text.  You can then use valign and halign attributes to change the text alignment withing the text_size.

Here is an example:

from kivy.app import App
from kivy.lang import Builder

kv = """
BoxLayout:
    orientation: 'vertical'
    AnchorLayout:
        Label:
            id: label
            size_hint: None, None
            size: dp(400), dp(400)
            text_size: self.size
            valign: 'center'
            halign: 'center'
            text: 'The Label Text'
            canvas.before:
                Color:
                    rgba: 0.2, 0.7, 0.3, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
    BoxLayout:
        size_hint_y: None
        height: dp(48)
        Spinner:
            text: 'valign'
            values: ['bottom', 'center', 'top']
            on_text: label.valign = self.text
        Spinner:
            text: 'halign'
            values: ['auto', 'left', 'center', 'right', 'justify']
            on_text: label.halign = self.text
"""


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


TextAlignmentApp().run()



WS

unread,
May 15, 2024, 9:28:01 PM5/15/24
to Kivy users support
Thanks ElliotGPT !

Elliot Garbus

unread,
May 15, 2024, 9:33:23 PM5/15/24
to kivy-...@googlegroups.com
lol! 
Sent from my iPhone

On May 15, 2024, at 6:28 PM, WS <wstech...@gmail.com> wrote:

Thanks ElliotGPT !
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1edc5b23-bbb7-4541-9bb7-be3d1128edd5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages