我在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")

发布于 2015-07-07 04:19:50
您可以使用其_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()https://stackoverflow.com/questions/31236504
复制相似问题