Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to receive a stream data from my host 127.0.0.1 port 23456 and send the data at my GUI in listiew so long as I do not want that GUI freeze, I use 2 threads, but I am not so much of an expert in Python.
This is my code:
Python
import tkinter as tk
from tkinter import ttk
import socket
import threading

def cell_selected(event):
    selected_item = tree.selection()[0]
    item_values = tree.item(selected_item, 'values')
    print("Selected item:", item_values)

def connect_action():
    threading.Thread(target=receive_data).start()

def exit_program():
    root.quit()

#Function to handle received data inside the the main thread
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(",")
                    
                    #Schedule GUI update in the main thread here...
                    root.after(0, handle_received_data, data_list)
                    
        except socket.error as e:
            print("Errore nella connessione:", e)

def update_table(event):
    data_list = event.data
    if data_list:
        tree.item(tree.get_children()[1], values=data_list)

root = tk.Tk()
root.title("Spreadsheet App")

# Creazione del widget Notebook (schede)
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)

# Primo tab con il primo Treeview
tab1 = ttk.Frame(notebook)
notebook.add(tab1, text="Terminal")

columns = ("Order","TimeOp","Type","Size","Symbol","Price","S/L","T/P",
           "Price","Com.","Swap","Profit","Comment","Close")

tree = ttk.Treeview(tab1, columns=columns, show="headings")
for col in columns:
    tree.heading(col, text=col)
    
tree.bind("<ButtonRelease-1>", cell_selected)
tree.pack(padx=10, pady=10)

# Pulsante "Connetti" a destra
connect_button = tk.Button(root, text="Connetti", command=connect_action)
connect_button.pack(side="right", padx=10, pady=5)

# Secondo tab con il secondo Treeview
tab2 = ttk.Frame(notebook)
notebook.add(tab2, text="Storico")

tree2 = ttk.Treeview(tab2, columns=("Symbol","Price","S/L"), show="headings")
# ... Aggiungi colonne e dati al secondo Treeview ...

tree2.bind("<ButtonRelease-1>", cell_selected)
tree2.pack(padx=10, pady=10)

exit_button = tk.Button(root, text="Esci", command=exit_program)
exit_button.pack(side="right", pady=5)

class CustomEvent:
    def __init__(self, data):
        self.data = data

def generate_custom_event(data):
    event = CustomEvent(data)
    root.event_generate("<<DataReceived>>", data=event)

root.bind("<<DataReceived>>", update_table)  # Bind custom event

root.mainloop()


What I have tried:

It returns me this error:

In ascolto su 127.0.0.1:23456
Connesso da ('127.0.0.1', 49967)
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
Posted
Updated 28-Aug-23 8:03am
v3
Comments
Richard MacCutchan 28-Aug-23 4:20am    
It is not clear which call has caused the exception. Was there more information in the stack trace?
GiulioRig 28-Aug-23 4:49am    
In ascolto su 127.0.0.1:23456
Connesso da ('127.0.0.1', 54769)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 861, in callit
func(*args)
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Exception in
Richard MacCutchan 28-Aug-23 5:02am    
The issue would appear to be caused by the code referred to in the following error message:
File "C:\Users\pct\Desktop\python\tabella.py", line 20, in handle_received_data
tree.item(tree.get_children()[1], values=data_list)
~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900