Your error is related to the way you're accessing the data of the custom event object. In your code you have created a custom event class called 'CustomEvent' to handle the custom data, but you are trying to access the data attribute of the event object directly, which is causing the error.
You need to modify the way you generate the custom event -
def generate_custom_event(widget, data):
event = CustomEvent(widget, data)
widget.event_generate("<<DataReceived>>", when="tail", data=event)
You need to modify the 'receive_data' function to use the 'generate_custom_event' function to send the received data as a custom event -
def receive_data():
host = "127.0.0.1"
port = 23456
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"In ascolto su {host}:{port}")
try:
conn, addr = s.accept()
with conn:
print(f"Connesso da {addr}")
while True:
data = conn.recv(1024)
if not data:
break
data_str = data.decode("utf-8")
data_list = data_str.split(",")
generate_custom_event(root, data_list)
except socket.error as e:
print("Errore nella connessione:", e)
In your 'update_table' function, you need to access the data attribute of the custom event object -
def update_table(event):
custom_event = event.data
data_list = custom_event.data
if data_list:
tree.item(tree.get_children()[1], values=data_list)
[EDIT]
If the error below in the comments is shown, it means you are trying handling threading in a 'Tkinter' application. 'Tkinter' is not thread-safe, meaning you can't update the GUI from a non-main thread directly. You need to ensure that the GUI updates are performed from the main thread. You need to use the 'root.after()' method to schedule updates in the main thread. You can learn more from -
How to Schedule an Action with Tkinter after() method[
^]
def handle_received_data(data_list):
if data_list:
tree.item(tree.get_children()[1], values=data_list)
def receive_data():
host = "127.0.0.1"
port = 23456
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"In ascolto su {host}:{port}")
try:
conn, addr = s.accept()
with conn:
print(f"Connesso da {addr}")
while True:
data = conn.recv(1024)
if not data:
break
data_str = data.decode("utf-8")
data_list = data_str.split(",")
root.after(0, handle_received_data, data_list)
except socket.error as e:
print("Errore nella connessione:", e)