Entry.get () gives an error, different each time. Invalid command name .55

advertisements

I am trying to make a simple chat window for school. When I try to use the entry.get() function it gives an error which changes every time I test the code. Here is the script:

import socket
import sys
import tkinter

global ready
ready = False
def setready():
    global ready
    ready = True
window = tkinter.Tk()
window.title("MAGENTA Chat Host v1 - send")
window.geometry("200x100")
window.configure(bg = "magenta")
but = tkinter.Button(window, text = "Send", command = (setready()))
lbl = tkinter.Label(window, bg = "magenta", fg = "snow", text = "Chat")
msgfield = tkinter.Entry(window)
but.pack(side = tkinter.RIGHT)
lbl.pack(side = tkinter.TOP)
msgfield.pack(side = tkinter.LEFT)
message = "Message"
UDP_IP = "127.0.0.1"

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lbl.config(text = "Socket successfully created")
except socket.error as err:
    lbl.config(text = "Socket creation failed with error %s" %(err))

window.mainloop()

while True:
    if ready == True:
        message = msgfield.get()
        if message == "exit":
            s.close()
            window.destroy()
            sys.exit()
        else:
            s.sendto(bytes(message, "utf-8"),(UDP_IP,5000))
            msgfield.select_clear()
            ready = False

Here is the error:

Traceback (most recent call last):
  File "C:\Users\qscguest\Desktop\ChatSend.py", line 33, in <module>
    message = msgfield.get()
  File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 2522, in get
    return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".55573472"

How did I mess up so badly? How do I fix this?


window.mainloop() won't exit until the root window is destroyed. Any code after that which tries to reference a widget will fail, since the widget has been destroyed.