I'm fairly new to Kivy, I've made a few small apps and stuff like that, but now I'm having a problem trying to create left-aligned typing effect text.
My idea was to have the text appear on the corner of the screen, similar to a terminal (I'll leave an example image taken from Pydroid). I was able to achieve this with static text, but for my project, I also needed a typing effect, which I also managed to implement. However, when trying to combine the two, I couldn't get the text to align to the left. It always stays in the middle.
My code:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.clock import Clock
class MyLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.text_to_type= self.text
self.text = ""
Clock.schedule_interval(self.type_text, 0.05)
def type_text(self, txt):
if len(self.text) < len(self.text_to_type):
self.text += self.text_to_type[len(self.text)]
class Example(App):
def build(self):
self.root = FloatLayout()
self.root.add_widget(MyLabel(text='''
That's a typing text!
And I'm trying to make it aligned!''',
size_hint=(1.0, 1.0),
halign="left",
valign="bottom"))
return self.root
if __name__ == "__main__":
Example().run()
How could I make it align to the left, looking like the example photo I posted? Thanks in advance!
You need to set the text_size
property of the Label
in order for the alignment to work (see the documentation). Here is a modified version of your code that does that:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.clock import Clock
class MyLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.text_to_type = self.text
self.text = ""
Clock.schedule_interval(self.type_text, 0.05)
def type_text(self, txt):
if len(self.text) < len(self.text_to_type):
self.text += self.text_to_type[len(self.text)]
kv = '''
<MyLabel>:
size_hint: (1.0, 1.0)
text_size: self.size
halign: "left"
valign: "bottom"
'''
class Example(App):
def build(self):
Builder.load_string(kv)
self.root = FloatLayout()
self.root.add_widget(MyLabel(text='''
That's a typing text!
And I'm trying to make it aligned!'''))
return self.root
if __name__ == "__main__":
Example().run()