首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取TextInput或标签的宽度(以字符为单位

如何获取TextInput或标签的宽度(以字符为单位
EN

Stack Overflow用户
提问于 2015-07-06 08:56:52
回答 1查看 1.3K关注 0票数 3

我在Kivy中有一个包含长内容的TextInput。我想知道TextInput的字符宽度。换句话说,这些线的长度是多少?

代码语言:javascript
复制
textinput = TextInput(text="Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps")

EN

回答 1

Stack Overflow用户

发布于 2015-07-07 04:19:50

您可以使用其_lines属性检查TextInput的行。要获得它们的长度,请使用len()内置:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31236504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档