2015-10-07 6 views
1

Я пытаюсь сделать простой сеанс чата. То, с чем я борюсь, состоит в том, чтобы заставить сообщение чата выравниваться влево и иметь динамический текст в соответствии с размером окна.Kivy Оболочка текста в виджет прокрутки

Идея состоит в том, чтобы сообщения чата отображались как метки в макете сетки.

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 

файл киловольт:

<Root>: 
    Screen: 
     name: 'main' 
     BoxLayout: 
      orientation: 'vertical' 
      Button: 
       text: '' 
       on_release: app.root.current = 'chat' 

    Screen: 
     name: 'chat' 
     BoxLayout: 
      orientation: 'vertical' 
      ScrollView: 
       id: scroller 
       do_scroll_x: False 
       GridLayout: 
        id: chatstream 
        cols: 1 
        padding: (10, 15) 
        spacing: 20 
        size_hint_y: None 
      Button: 
       text: '' 
       on_release: app.root.current = 'main' 

ответ

1

Установить размер ваших экземпляров ChatMessage, например

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

Также удалите параметр text_size в размере python. Вы можете полностью удалить __init__, если вы установили halign в kv тоже.

Смежные вопросы