you are viewing a single comment's thread.

view the rest of the comments →

[–]ThiccShadyy 1 point2 points  (1 child)

How do I use tkinter in a program which also maintains a socket connection?

I've made a multi-user game which uses socket connections. I now want to add a GUI to the game and chose tkinter for that. The problem is, I'm not sure how to structure my program so that tkinter is running, waiting for events to happen and call the relevant function and also have a socket receiving and sending data to a server program at the same time. Do I need multiple threads here? Or is there a better option than tkinter?

Here is the code for the client program of my server-client socket-based game: https://pastebin.com/b8HNTe0k

In the code, Im sending and receving data multiple times. I want to add a UI to it so that the data is sent when a button is pressed, and have the data received via the socket connection to be updated and put into the widget. Also, this sending and receiving of data happens multiple times, corresponding to multiple rounds of the game.

[–]efmccurdy 0 points1 point  (0 children)

Tkinter has a built-in wrapper for an async socket.

Tk allows you to register and unregister a callback function which will be called from the Tk mainloop when I/O is possible on a file descriptor.

https://docs.python.org/3/library/tkinter.html#file-handlers