You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.5 KiB
40 lines
1.5 KiB
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
class ChatApp:
|
|
def __init__(self, master):
|
|
self.master = master
|
|
master.title("Chat App")
|
|
master.geometry("500x600")
|
|
|
|
self.chat_history = tk.Text(master, height=25, width=60, state="disabled")
|
|
self.chat_history.tag_configure("right", justify="right")
|
|
self.chat_history.tag_configure("left", justify="left")
|
|
self.chat_history.pack(side="top", fill="both", expand=True)
|
|
|
|
self.message_input = tk.Entry(master, width=60)
|
|
self.message_input.pack(side="left", padx=5, pady=5, expand=True, fill="both")
|
|
|
|
send_button_image = tk.PhotoImage(file="send.png")
|
|
self.send_button = tk.Button(master, image=send_button_image, command=self.send_message)
|
|
self.send_button.image = send_button_image
|
|
self.send_button.pack(side="right", padx=5, pady=5)
|
|
|
|
self.message_input.bind("<Return>", self.send_message)
|
|
|
|
def send_message(self, event=None):
|
|
message = self.message_input.get()
|
|
self.message_input.delete(0, "end")
|
|
self.display_message("Me", message, "right")
|
|
|
|
def display_message(self, name, message, justify):
|
|
self.chat_history.configure(state="normal")
|
|
self.chat_history.insert("end", f"{name}:\n", justify)
|
|
self.chat_history.insert("end", f"{message}\n")
|
|
self.chat_history.configure(state="disabled")
|
|
|
|
if __name__ == '__main__':
|
|
root = tk.Tk()
|
|
chat_app = ChatApp(root)
|
|
root.mainloop()
|