0

I'm doing a chatroom type thing and I'm want to have messages to appear on the textbox in the GUI but I'm not sure what to add into the GUI code.

I've tried a few things and I know i need to put something in the while loop at the bottom of the gui but I'm not sure what needs to go there. I believe it would be some sort of while try loop that sends the results to the textbox. All my attempts got blank results so I'm a bit stuck. SERVER CODE:

# imports
import socket
import threading


def set_address_and_port():
    # IP ADDRESS & SERVER PORT (These change around alot at the moment)
    IP = input("Please input your IP: ")
    port = int(input("Please input your port: "))
    global SERVER_ADDRESS, SERVER_PORT
    SERVER_ADDRESS = IP
    SERVER_PORT = port
    return SERVER_ADDRESS, SERVER_PORT


# This only does SINGLE CLIENT
def sent_message():
    from client import send_message
    sent = send_message.full_message
    sent.get()


def handle_client(client_socket, client_address):
    while True:
        message = client_socket.recv(1024).decode('utf-8')
        if not message:
            break
        print(f"Received from {client_address[0]}:{client_address[1]}: {message}")

    client_socket.close()


# starts the server again
def start_server():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((SERVER_ADDRESS, SERVER_PORT))
    server_socket.listen(5)
    print(f"Server is listening on {SERVER_ADDRESS}:{SERVER_PORT}")

    while True:
        client_socket, client_address = server_socket.accept()
        print(f"Client connected: {client_address[0]}:{client_address[1]}")
        client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address))
        client_thread.start()


if __name__ == '__main__':
    set_address_and_port()
    start_server()

GUI CODE:

import customtkinter as ctk
import socket
import threading



SERVER_ADDRESS = '10.0.36.9'
SERVER_PORT = 1234

chat = ctk.CTk()
chat.geometry('900x600')
ctk.set_appearance_mode('light')

def handle_return_key(event):
    send_message()



def send_message():
    message = message_entry.get()
    message_entry.delete(0, len(message))

    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((SERVER_ADDRESS, SERVER_PORT))
    client_socket.send(message.encode('utf-8'))

    from server import sent_message
    textbox.insert("1.0", sent_message)


chat.columnconfigure(0, weight=1)
chat.columnconfigure(1, weight=1)
chat.columnconfigure(2, weight=1)

chat.rowconfigure(0, weight=0)
chat.rowconfigure(1, weight=1)
chat.rowconfigure(2, weight=2)
chat.rowconfigure(3, weight=3)
chat.rowconfigure(4, weight=0)

message_entry = ctk.CTkEntry(master=chat, placeholder_text="Message Here...", width=300)
message_entry.bind(sequence = '<Return>', command=handle_return_key)

message_entry.grid(row=4, column=2, columnspan=1, sticky="ew", pady=20)

send_button = ctk.CTkButton(chat, text="send", command=send_message, hover=True, width=100)
send_button.grid(row=4, column=3, columnspan=1, padx=20, pady=20, sticky="ew")

textbox = ctk.CTkTextbox(chat)
textbox.configure(state="disabled", text_color="black")  # configure textbox to be read-only
textbox.grid(row=0, column=2, columnspan=2, rowspan=4, padx=20, pady=20, sticky='nsew')



while True:
    # insert code for receiving new network messages
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((SERVER_ADDRESS, SERVER_PORT))
    client_socket.setblocking(False)
    client_socket.recv(1024).decode('utf-8')


    chat.update_idletasks()
    chat.update()
# chat.mainloop()

'''
  • Does it help [How to make tkinter repond events while waiting socket data?](https://stackoverflow.com/questions/3348757/how-to-make-tkinter-repond-events-while-waiting-socket-data) – Armali Aug 11 '23 at 08:29

0 Answers0