在滚动视图中按下按钮时添加标签

时间:2018-12-28 19:27:05

标签: python-3.x kivy-language

## Python文件##

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

    class Chat(Widget):
        pass

        def __init__(self):
            super(Chat,self).__init__()



    class ChatApp(App):
        def post(self,msg):

            if len(msg)>0:
                msgbox=Chat()
                msgbox.ids.mlab.text=msg
                self.root.ids.chatbox.add_widget(msgbox)   #add widget when a button is pressed
                self.root.ids.text_inpt.text=''   #after adding the widget clear the textinput widget's text

        def build(self):
            return Chat()


  myapp = ChatApp()
      myapp.run()
  1. 每当按下文本输入中的文本时,我都试图添加标签。

  2. 相反,它会引发错误或在屏幕上不显示任何内容。

  3. 此外,我想添加另一种使其成为聊天屏幕的方法。因此,为此,我需要使输入标签向右对齐,响应标签向左对齐。

    谢谢您的帮助!


KV文件

<Label>:
    center: root.center
    padding: (-10,0)
    markup: True
    text_size: (None, None)
    valign: 'top'
    size_hint: (1, None)
    color: 0, 0, 0

<BoxLayout>:
    padding: 10
    spacing: 10

<Button>:
    font_size: 30
    height: 60
    size_hint: (1, None)
    border: (1, 1, 1, 1)

<TextInput>:
    font_size: 20
    multiline: True
    padding: [10, 0.5 * (self.height - self.line_height)]


<Chat@Widget>:
    Label:
        id: mlab
        padding: 10, 10
        markup: True
        text_size: (None, None)
        text: ''
        haligh: 'left'
        valign: 'top'
        size_hint: (1, None)
        size: self.texture_size
        color: 0, 0, 0
    BoxLayout:

        orientation: 'vertical'
        size:root.size
        ScrollView:

            id:scrlv

            canvas.before:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

            StackLayout:
                id:chatbox
                padding: 10, 10
                orientation: 'tb-rl'


        BoxLayout:
            height: 60
            orientation: 'horizontal'
            padding: 0
            size_hint: (1, None)

            TextInput:
                id:text_inpt

            Button:
                text: 'Send'
                size_hint: (0.3, 1)
                on_release: app.post(text_inpt.text)

0 个答案:

没有答案