Kivy TextInput 如何从右侧开始输入
Posted
技术标签:
【中文标题】Kivy TextInput 如何从右侧开始输入【英文标题】:Kivy TextInput how can start typing from the right 【发布时间】:2019-03-13 00:33:38 【问题描述】:我正在用 kivy 构建一个简单的计算器,如何让光标自动从右到左开始输入
【问题讨论】:
发布您尝试过的代码 【参考方案1】:不幸的是,事情没那么简单。不确定这是否正是您正在寻找的,但这是我为类似案例编写的TextInput
的扩展。它使文本保持正确调整。请注意,这期望文本是合法的浮点数。如果您不想要该限制,只需删除调用 float
的代码:
class FloatInputRight(TextInput):
def __init__(self, **kwargs):
super(FloatInputRight, self).__init__(**kwargs)
self.multiline = False
def right_adjust(self, text):
if text == '':
return text
max_width = self.width - self.padding[0] - self.padding[2]
new_text = text
text_width = self._get_text_width(new_text, self.tab_width, self._label_cached)
while text_width < max_width:
new_text = ' ' + new_text
text_width = self._get_text_width(new_text, self.tab_width, self._label_cached)
while text_width >= max_width:
if new_text[0] != ' ':
break
else:
new_text = new_text[1:]
text_width = self._get_text_width(new_text, self.tab_width, self._label_cached)
return new_text
def on_size(self, instance, value):
super(FloatInputRight, self).on_size(instance, value)
if len(self._lines) == 0:
return True
cc, cr = self.cursor
cur_text = self._lines[cr]
initial_len = len(cur_text)
super(FloatInputRight, self)._refresh_text(self.right_adjust(cur_text))
final_len = len(self._lines[cr])
self.cursor = self.get_cursor_from_index(final_len - (initial_len - cc))
return True
def delete_selection(self, from_undo=False):
if not self._selection:
return
cr = self.cursor[1]
initial_len = len(self._lines[cr])
a, b = self._selection_from, self._selection_to
if a > b:
a, b = b, a
super(FloatInputRight, self).delete_selection(from_undo=from_undo)
cur_text = self._lines[cr]
super(FloatInputRight, self)._refresh_text(self.right_adjust(cur_text))
final_len = len(self._lines[cr])
self.cursor = self.get_cursor_from_index(final_len - (initial_len - b))
def do_backspace(self, from_undo=False, mode='bkspc'):
cc, cr = self.cursor
initial_len = len(self._lines[cr])
super(FloatInputRight, self).do_backspace(from_undo=from_undo, mode=mode)
cc, cr = self.cursor
cur_text = self._lines[cr]
super(FloatInputRight, self)._refresh_text(self.right_adjust(cur_text))
final_len = len(self._lines[cr])
self.cursor = self.get_cursor_from_index(final_len - (initial_len-cc) + 1)
def insert_text(self, the_text, from_undo=False):
cc, cr = self.cursor
cur_text = self._lines[cr]
initial_len = len(cur_text)
new_text = self.right_adjust(cur_text[:cc] + the_text + cur_text[cc:])
try:
num = float(new_text) # throw exception if new_text is invalid float
except ValueError:
return
self._lines[cr] = ''
super(FloatInputRight, self).insert_text(new_text, from_undo=from_undo)
final_len = len(self._lines[cr])
self.cursor = self.get_cursor_from_index(final_len - (initial_len-cc))
def set_right_adj_text(self, text):
num = float(text) # throws exception if text is invalid float
self._refresh_text(self.right_adjust(text))
【讨论】:
以上是关于Kivy TextInput 如何从右侧开始输入的主要内容,如果未能解决你的问题,请参考以下文章
单击“TextInput”对象后在 Kivy 中重新获得键盘焦点
Kivy:如何在 kivy 中制作圆角 TextInput?