Tkinter using mainloop and another loop

65 Views Asked by At

I'm doing a project where I read info from a socket and then intend to display it on a gui using tkinter. The thing is, my read info from socket is a loop and for the gui I need another loop.

I'm pretty inexperienced with both Python and Tkinter, which probably explains my mistake here.

The fd_dict is a dictionary with the properties and respective values of a car ex: gear, power, speed, etc (theme of my project).

The main problem is either I get the values from the socket or I display the gui, never both obviously, since it stays on the earlier loop.

while True:
    # UDP server part of the connection
    message, address = server_socket.recvfrom(1024)
    del address
    fdp = ForzaDataPacket(message)
    fdp.wall_clock = dt.datetime.now()

    # Get all properties
    properties = fdp.get_props()
    # Get parameters
    data = fdp.to_list(params)
    assert len(data) == len(properties)
    # Zip into a dictionary
    fd_dict = dict(zip(properties, data))
    # Add timestamp
    fd_dict['timestamp'] = str(fdp.wall_clock)
    
    # Print of various testing values
    print('GEAR: ', fd_dict['gear'])
    print('SPEED(in KMH): ', fd_dict['speed'] * 3.6) #speed in kph
    print('POWER(in HP): ', fd_dict['power'] * 0.0013596216173) #power in hp
    #print('PERFORMANCE INDEX: ', fd_dict['car_performance_index'])
    print('\n')

The tkinter code:

window = Tk()
window.title('Forza Horizon 5 Telemetry')
window.geometry("1500x800")
window.configure(bg="#1a1a1a")

frame = Frame(window)
frame.pack()

label_gear = Label(text = '0')
label_gear.configure(bg="darkgrey")
label_gear.pack()

I read about using after() and using classes, but I've never used them, and can't figure out how to apply them here.

Thanks in advance.

0

There are 0 best solutions below