0

So I'm doing a project in school and I'm making a game with Tkinter (I know I could have used PyGame but We learned only Tkinter in school....) My game using sockets as well and as you might know once the code is stuck on RECEIVE the whole program freezes. What I did in order to get over this problem is that most of the time that I RECEIVE something from the server I do it in a Thread. but since I cannot move on without the server's answer I called MainLoop so the program will "stop" on it and once I got the information from the client I destroyed the MainLoop and continued where I needed.... My question is if it's OK to do so and if not please help me get over this problem in a "nice" way.

It looks something like that:

global data
Thread(target=recv)
def recv():
    global data
    data = my_socket.rceive(1024)
tk.mainlooop()
if data == True:
    ...Do Something...
Red
  • 26,798
  • 7
  • 36
  • 58
Adam
  • 11
  • 1
  • First you have to understand [Event-driven_programming](https://en.m.wikipedia.org/wiki/Event-driven_programming). Read through [`[tkinter] event driven programming`](https://stackoverflow.com/search?q=is%3Aanswer+%5Btkinter%5D+event+driven+programming+entry) and [Tkinter understanding mainloop](https://stackoverflow.com/a/29158947/7414759) – stovfl May 14 '20 at 12:37

1 Answers1

-2

Try this

while true:
    Try:
        data = my_socket.rceive(1024)
        break
    Else:
        print("Retrying")
  • A While Loop is also stopping the rest of the code.... It will behave just like a reguar socket.receive() since in order the GUI to continue the code must be in the MainLoop line – Adam May 14 '20 at 12:31