Kivy标签文本自动换行滚动窗口

2024-09-29 05:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个简单的聊天流。我正在努力的是让聊天消息左对齐,并根据窗口大小动态包装文本。在

这个想法是让聊天信息在网格布局中显示为标签。在

class ChatMessage(Label):

    def __init__(self, *args, **kwargs):
        super(ChatMessage, self).__init__(*args, **kwargs)
        self.text_size = self.size
        self.halign = 'left'

class Root(ScreenManager):

    def __init__(self, *args, **kwargs):
        super(ScreenManager, self).__init__(*args, **kwargs)
        self.ids.chatstream.bind(minimum_height=self.ids.chatstream.setter('height'))
        self.add_message()


    def add_message(self):
        for x in xrange(50):
            label = ChatMessage(text='Im still not completely sure I understand the mechanisms at work here, though.. Im still not completely sure I understand the mechanisms at work here, though.. Im still not completely sure I understand the mechanisms at work here, though.' + str(x))

            self.ids.chatstream.add_widget(label)
            self.ids.scroller.scroll_y = 0

kv文件:

^{pr2}$

Tags: selfaddidsinitdefnotargskwargs
1条回答
网友
1楼 · 发布于 2024-09-29 05:29:52

设置聊天消息实例的大小,例如

<ChatMessage>
    size_hint_y: None
    height: self.texture_size[1]  # the size needed to fit the text
    text_size: self.width, None

同时删除python大小中的text_size设置。如果将halign设置为kv,则可以完全删除__init__。在

相关问题 更多 >