Real-Time Desktop Chat App
Real-Time Desktop Chat App
This application will use the classes for socket communication we created earlier.
Create a new file chat_gui.py in the same folder as the socket_server.py and
socket_client.py . This app uses the customtkinter package so be sure to install it before you
get started.
ctk.set_appearance_mode("light")
ctk.set_default_color_theme("dark-blue")
class ChatApp(ctk.CTk):
def __init__(self, messages=None, user={"username": "Prince", "id": 1},
width=300, server_host=None, server_port=None):
super().__init__()
self.username = user['username']
self.id = user['id']
self.width = width
# input box for a message
self.entry = ctk.CTkEntry(master=self, width=300)
self.entry.grid(row=1, column=0, pady=10)
self.entry.focus()
self.create_widgets()
def create_widgets(self):
for index, message in enumerate(self.messages):
self.display_message(message, index)
# put frame on the left if the message was not sent by the user
frame = ctk.CTkFrame(master=self.messages_frame, width=self.width/2)
frame.grid(row=index, column=0 if not is_message_sent_by_user else 1,
padx=10, pady=10)
if __name__ == '__main__':
username = input("What's your username? ")
userid = input("Provide your id: ")
app = ChatApp(
user={"username": username, "id": userid},
server_host="localhost", server_port=12345
)
app.mainloop()