Python Kivy: align text on left side of label

I have read the documents and still do not know how to align the text inside the Kivy-Label on the left side. Text has a default value. A halign = "left"did not help. Sorry if the solution is obvious, but I just can't find it.

EDIT: Code Example:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label

class Example(App):
     def build(self):
          self.root = FloatLayout()
          self.label = Label(text="I'm centered :(", pos=(0,0), size_hint=(1.0,1.0), halign="left")
          self.label.text_size = self.label.size #no horizontal change
          self.root.add_widget(self.label)
          return self.root

Example().run()
+4
source share
1 answer

According to the documentation , it looks like the newly created shortcut has a size that exactly matches the length of the text, so you may not see any difference after setting the halign property.

It is recommended to change the size property (as shown in the example)

text_size = self.size

which sets the size of the label on the widget containing it. Then you should see that the label is correctly centered.

Tshirtman, text_size size. :

#!/usr/bin/kivy
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label

class Example(App):
    def build(self):
        self.root = FloatLayout()
        self.label = Label(text="I'm aligned :)", size_hint=(1.0, 1.0), halign="left", valign="middle")
        self.label.bind(size=self.label.setter('text_size'))    
        self.root.add_widget(self.label)
        return self.root

Example().run()
+13

Source: https://habr.com/ru/post/1599739/


All Articles