Tkinter can't work while a socket loop is happening Tkinter can't work while a socket loop is happening tkinter tkinter

Tkinter can't work while a socket loop is happening


You are correct. In essence, you need two infinite loops running in parallel: you need one to service the GUI, and another to service the socket server. Your only option is to use two threads or two processes.*

The main thing to keep in mind is that Tkinter needs to run in the main thread, and you can't directly access Tkinter objects from the other thread since Tkinter isn't thread safe. You will need to introduce some sort of message passing mechanism if you want the socket thread to interact with the Tkinter

*those aren't your only options, but they are the most practical. When I need to read data from a socket I usually write some Tcl code to run in the underlying Tcl interpreter since Tcl's fileevent model is so powerful. You lose the complexity of a threaded implementation, at the expense of having to do a little Tcl programming. But I digress.


Well boys i just use Threaring now.Look:

t = Thread(target=start_server)t.start()

But thanks for the answer.


You may want to try the multiprocessing module:

import multiprocessingdef start():   def start_tkinter():      global main,l      # ...      main.update()      proc = multiprocessing.Process(target=start_server)      proc.start()   def start_server():     HOST, PORT = "localhost", 9999     # ...

However, since we do not know what you want to do neither your code is really executable as given in the question, I cannot just give this suggestion.