## 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()
每当按下文本输入中的文本时,我都试图添加标签。
相反,它会引发错误或在屏幕上不显示任何内容。
此外,我想添加另一种使其成为聊天屏幕的方法。因此,为此,我需要使输入标签向右对齐,响应标签向左对齐。
谢谢您的帮助!
<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)