gpt4 book ai didi

python - 如何获取 Kivy TextInput 或 Label 的字符宽度

转载 作者:行者123 更新时间:2023-12-04 18:03:29 24 4
gpt4 key购买 nike

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

enter image description here

最佳答案

您可以使用 _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()

关于python - 如何获取 Kivy TextInput 或 Label 的字符宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com