我在Kivy
中有一个长内容的TextInput
。我想知道TextInput
的字符宽度,也就是行的长度?
textinput = TextInput(text="Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps")
_lines
属性检查TextInput
的行。要获取它们的长度,请使用len()
内置函数:from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string("""
<MyWidget>:
Button:
text: "Print width"
on_press: print([len(line) for line in ti._lines])
TextInput
text: "Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps"
id: ti
""")
class MyWidget(BoxLayout):
pass
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
TextInput._lines_labels
中,有一个字段大小(元组)[0],它表示行的宽度。每一行都是如此。只需迭代并寻找最大值,然后'voilà'。
这适用于任何字体(prop或非prop)。
cursor_col
和cursor_row
怎么样:http://kivy.org/docs/api-kivy.uix.textinput.html#kivy.uix.textinput.TextInput.cursor_col? - Iron Fistcursor_col
和cursor_row
之间进行组合检查,以存储cursor_col
的值和/或下一个新行时的值。当cursor_row
= +1 时,那就应该是该行的长度。你明白我的意思吗? - Iron Fist