-
-
Save MaddoxRauch/53e16279ddf7df47664bfb90e3b58a74 to your computer and use it in GitHub Desktop.
Kivy Aligned Text Input - halign + valign for TextInput
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.uix.textinput import TextInput | |
from kivy.properties import StringProperty | |
import functools | |
DEFAULT_PADDING = 6 | |
class AlignedTextInput(TextInput): | |
halign = StringProperty('left') | |
valign = StringProperty('top') | |
def __init__(self, **kwargs): | |
self.halign = kwargs.get("halign", "left") | |
self.valign = kwargs.get("valign", "top") | |
self.bind(on_text=self.on_text) | |
super().__init__(**kwargs) | |
def on_text(self, instance, value): | |
self.redraw() | |
def on_size(self, instance, value): | |
self.redraw() | |
def redraw(self): | |
""" | |
Note: This methods depends on internal variables of its TextInput | |
base class (_lines_rects and _refresh_text()) | |
""" | |
self._refresh_text(self.text) | |
total_size = (x.size for x in self._lines_rects) # Modified to handle runtime dynamic on_text | |
max_size = functools.reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], total_size) | |
num_lines = len(self._lines_rects) | |
px = [DEFAULT_PADDING, DEFAULT_PADDING] | |
py = [DEFAULT_PADDING, DEFAULT_PADDING] | |
if self.halign == 'center': | |
d = (self.width - max_size[0]) / 2.0 - DEFAULT_PADDING | |
px = [d, d] | |
elif self.halign == 'right': | |
px[0] = self.width - max_size[0] - DEFAULT_PADDING | |
if self.valign == 'middle': | |
d = (self.height - max_size[1] * num_lines) / 2.0 - DEFAULT_PADDING | |
py = [d, d] | |
elif self.valign == 'bottom': | |
py[0] = self.height - max_size[1] * num_lines - DEFAULT_PADDING | |
self.padding_x = px | |
self.padding_y = py | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note: This just aligns the position of the textblock, not the text itself. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kivy | |
from kivy.app import App | |
from kivy.uix.gridlayout import GridLayout | |
from . import AlignedTextInput | |
class AlignedTextInputApp(App): | |
def build(self): | |
layout = GridLayout(cols=3) | |
layout.add_widget(self.get_input('top', 'left')) | |
layout.add_widget(self.get_input('top', 'center')) | |
layout.add_widget(self.get_input('top', 'right')) | |
layout.add_widget(self.get_input('middle', 'left')) | |
layout.add_widget(self.get_input('middle', 'center')) | |
layout.add_widget(self.get_input('middle', 'right')) | |
layout.add_widget(self.get_input('bottom', 'left')) | |
layout.add_widget(self.get_input('bottom', 'center')) | |
layout.add_widget(self.get_input('bottom', 'right')) | |
return layout | |
def get_input(self, v, h): | |
return AlignedTextInput(text='Example Text', halign=h, valign=v) | |
if __name__ == '__main__': | |
AlignedTextInputApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment