0

I would like for my client GUI to put the received messages into the chat box however due to part of the GUI not being in the main thread its giving me this error.

RuntimeError: main thread is not in main loop

Client code:

#imports
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('192.168.0.27',5006))

class GUI():

    def __init__(self):
        #constructor code

    def Structure(self):
        #appearance code
        self.Cons.config(state = DISABLED)
        self.receive_thread = Thread(target=GUI.receive, args=(self,))
        self.receive_thread.start()

    def receive(self):
        while True:
            msg = s.recv(1024)
            received = msg.decode("utf-8")
            print(received)
            self.Cons.config(state = NORMAL)
            self.Cons.insert(END, received+"\n\n")
            self.Cons.config(state = Disabled)
            self.Cons.see(END)

    def transmit(data,self):
        encodeddata=data.encode("utf-8")
        s.send(encodeddata)
        self.Msg.delete(0, 'end')


x=GUI()
x.Structure()
martineau
  • 112,593
  • 23
  • 157
  • 280
  • 1
    You shouldn't call `tkinter` functions from threads other than the one where you created your `Tk()` window. Some parts of `tkinter` aren't threadsafe – TheLizzard Jun 19 '21 at 17:24
  • 1
    There are ways to workaround the fact that `tkinter` isn't threadsafe, and one very common way to to use a `Queue` to allow threads to communicate with the one running the GUI, which can check for data in the queue periodically via the universal widget method [`after()`](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html). You can find numerous examples of doing that if you do some searching on this site. – martineau Jun 19 '21 at 18:32

0 Answers0