gpt4 book ai didi

python - 如何在每次发生事件时创建一个新的屏幕实例

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:01 25 4
gpt4 key购买 nike

每次有人向新人发送消息时,我想创建一个聊天屏幕的新实例,我创建了一个按钮 ListView ,并且每当有人向新人发送消息时它都会添加一个按钮,但我正在尝试找出如何将其定向到包含所有交互消息的独特聊天页面。任何帮助,将不胜感激。下面是我的代码

class Chat(Screen):

def on_enter(self, *args):
app = App.get_running_app()
self.chatdestination = Label(
text=firebase.get("/users/" + app.localId, 'first name') + " " + firebase.get('/users/' + app.localId,
'last name'),
pos_hint={"center_x": 0.5, "center_y": 0.95}, size_hint=[0.8, 0.1], color=(0, 0, 0, 1),
font_name="fonts/Qanelas-Light.otf")

self.add_widget(self.chatdestination)


def __init__(self, **kwargs):
super(Chat, self).__init__(**kwargs)
self.localId = None
self.messagebutton = Button(text="Send", font_size = 20, font_name= "fonts/Qanelas-Heavy.otf", background_color= (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint= {"right": 1,"down": 1}, size_hint= [0.2, 0.1])
self.messagebutton.bind(on_release = self.send_message)
self.add_widget(self.messagebutton)


self.messagetextinput = TextInput(width=Window.size[0]*0.8, hint_text= "Write a message", font_name= "fonts/Qanelas-Light.otf", size_hint= [0.8, 0.1], pos_hint= {"left": 1,"down": 1})
self.add_widget(self.messagetextinput)



Window.bind(on_key_down=self.on_key_down)

Clock.schedule_once(self.focus_text_input, 1)

def on_key_down(self, instance, keyboard, keycode, text, modifiers):
if keycode == 40:
self.send_message(None)



def send_message(self, _):
app = App.get_running_app()
message = self.messagetextinput.text
if len(message) > 0:
database.child("messages").child(app.localId).update({self.localId: message})
self.chatbubble = Button(text=self.messagetextinput.text, background_color = [0.082, 0.549, 0.984, 1.0], background_normal = "",
pos_hint= {"center_x": 1}, color = (1, 1, 1, 1), size_hint_y = None)
self.chatbubble.size = self.chatbubble.texture_size

self.ids.chatlayout.add_widget(self.chatbubble)
self.ids.scrollview.scroll_to(self.chatbubble)
self.messagetextinput.text = ""


Clock.schedule_once(self.focus_text_input, 0.1)


messages_list = []
if app.localId not in messages_list:
messages_list.append(app.localId)
for id in messages_list:
messages_screen = self.manager.get_screen('messages')
messages_screen.messages_list.adapter.data.extend([])

最佳答案

您应该研究一下 RecycleViews。我认为最好的选择可能是使用“聊天”作为 View 类的 RecycleView。

关于python - 如何在每次发生事件时创建一个新的屏幕实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com