def send_message(message, border_size=3, font_size=12): Это для отправки сообщения
если нет message.strip(): # Игнорировать пустые сообщения
return
Код: Выделить всё
user_entry.delete(0, tk.END) # Clear the entry box
# Insert user message aligned to the right with border and padding
chat_log.config(state=tk.NORMAL)
user_frame = tk.Frame(chat_log, bg="lightblue", bd=border_size, relief="solid")
user_label = tk.Label(user_frame, text="You: " + message, bg="lightblue", font=("Arial", font_size)) `In this part i tried to put the user's reponses to the right side`
user_label.pack(padx=5, pady=5)
chat_log.window_create(tk.END, window=user_frame, align='bottom', padx=10, pady=5) `This is the users responses`
chat_log.insert(tk.END, "\n", "right")
chat_log.config(state=tk.DISABLED)
# Insert Alice message aligned to the left with border and padding
chat_log.config(state=tk.NORMAL)
alice_frame = tk.Frame(chat_log, bg="lightgreen", bd=border_size, relief="solid")
alice_label = tk.Label(alice_frame, text="Alice: " + message, bg="lightgreen", font=("Arial", font_size)) `This is Alice responses which stays on the left side`
alice_label.pack(padx=5, pady=5)
chat_log.window_create(tk.END, window=alice_frame, align='top', padx=10, pady=5)`this is the Alice's responses`
chat_log.insert(tk.END, "\n", "left")
chat_log.config(state=tk.DISABLED)
# Scroll to the bottom to show the newest message
chat_log.see("end")
chat_frame = tk.Frame(root)
chat_frame .pack(expand=True, fill="both", Padx=10, Pady=10)
chat_log = tk.Text(chat_frame, Wrap=tk.WORD, state=tk. ОТКЛЮЧЕНО, bg="lightgray")
chat_log.pack(expand=True, fill="both", Padx=5, Pady=5)
полоса прокрутки = tk.Scrollbar (chat_frame, команда=chat_log.yview)
scrollbar.pack(side=tk.RIGHT, fill="y")
chat_log.config(yscrollcommand=scrollbar.set)
user_entry = tk.Entry(root, width=50)
user_entry.pack(side=tk.LEFT, Padx=10, Pady=10, fill=tk.X ,expand=True)
user_entry.bind("", on_enter) # Привязываем клавишу Enter к функции send_message
send_button = tk.Button(root, text= "Отправить", команда=лямбда: send_message(user_entry.get()))
send_button.pack(side=tk.LEFT, Padx=10, Pady=10)
clear_button = tk.Button(root, text="Очистить чат", command=clear_chat)
clear_button.pack(side=tk.LEFT,padx=10,pady=10)
< h1>Настройка выравнивания и шрифта текстовых тегов
chat_log.tag_configure("right", justify="right", font=('Arial', 12, 'bold'))
chat_log.tag_configure("left", justify="left", font=('Arial', 12, 'bold'))
Подробнее здесь: https://stackoverflow.com/questions/785 ... onses-cant