How to move text upwards on the screen every time a button is pressed

前端 未结 2 1342
粉色の甜心
粉色の甜心 2021-01-16 10:32

I want to develop a chatting application for android. It will receive messages from the controller on the site. For this i have started designing a GUI. After practicing and

2条回答
  •  终归单人心
    2021-01-16 10:52

    Chat App - Suggestion

    Kivy Label » Text alignment and wrapping

    • Declare a custom widget with inheritance of Label widget so that you can control the text alignment i.e. left for text sent, and right for text received.

    Snippets - kv file

    :
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
        halign: 'left'
        valign: 'middle'
    

    Kivy RecycleView

    • Replace Label: with RecycleView:
    • Use CustomLabel as the viewclass
    • Append text sent or text received to RecycleView's data

    Snippets - kv file

    RecycleView:
        id: rv
        viewclass: 'CustomLabel'
    
        RecycleBoxLayout:
            default_size_hint: 1, None
            orientation: 'vertical'
    

    Kivy RecycleView » data

    The view is generatad by processing the data, essentially a list of dicts, and uses these dicts to generate instances of the viewclass as required.

    data
    

    The data used by the current view adapter. This is a list of dicts whose keys map to the corresponding property names of the viewclass.

    data is an AliasProperty that gets and sets the data used to generate the views.

    Snippets - Py file

        def display_txt(self):
            self.ids.rv.data.append({'text': self.ids.txt.text, 'halign': 'left'})
            self.ids.txt.text = ''
    

    Example

    main.py

    from kivy.app import App
    from kivy.uix.screenmanager import Screen
    from kivy.lang import Builder
    
    
    class SMS(Screen):
        def send_txt(self):
            self.ids.rv.data.append({'text': self.ids.txt.text, 'halign': 'left'})
            self.ids.txt.text = ''
    
        def receive_txt(self):
            self.ids.rv.data.append({'text': self.ids.txt.text, 'halign': 'right'})
            self.ids.txt.text = ''
    
    
    Builder.load_file("main.kv")
    
    
    class MyApp(App):
        def build(self):
            return SMS()
    
    
    if __name__ == "__main__":
        MyApp().run()
    

    main.kv

    :
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
        halign: 'left'
        valign: 'middle'
    
    :
        GridLayout:
            cols : 1
    
            BoxLayout:
                size_hint: 1, 0.1
                Button:
                    text : 'Send'
                    on_press : root.send_txt()
                Button:
                    text : 'Receive'
                    on_press : root.receive_txt()
    
            TextInput:
                hint_text : 'Write here'
                id : txt
                size_hint : .8 , .1
                pos : 0 ,0
    
            RecycleView:
                id: rv
                viewclass: 'ChatBox'
    
                RecycleBoxLayout:
                    default_size_hint: 1, None
                    orientation: 'vertical'
    

    Output

提交回复
热议问题