简体   繁体   中英

Python send websocket message in tkinter

I am having trouble sending a websocket message in tkinter on python 2.7. I currently have tkinter and a websocket client running in a thread so they both loops work simultaneously, as seen here . The problem is that whenever I try typing a message and pressing the send button, I get a NameError. The websocket client that I am using is here .

This is my code:

from Tkinter import *
from websocket import *
from threading import *

master = Tk()
master.wm_title("Websocket Test")
minwidth = master.winfo_screenwidth()/4*3
minheight = master.winfo_screenheight()/4*3
master.minsize(width=minwidth, height=minheight)
master.resizable(0,0)

def sendmsg():
   msg = entry.get()
   ws.send(msg)
   return

text = Text(master)
text.pack(side=TOP, expand=True,fill=BOTH)
entry = Entry(master)
entry.pack(side=BOTTOM, expand=True, fill=BOTH)
button = Button(master, text="SEND", command=sendmsg)
button.pack(side=BOTTOM, expand=True, fill=BOTH)

def on_message(ws, message):
   text.insert(END, "Received: "+message+"\n")
   print "Received: "+message
   return

def on_error(ws, error):
   text.insert(END, error+"\n")
   print error
   return

def on_close(ws):
   text.insert(END, "### closed ###\n")
   print "### closed ###"
   return

def on_open(ws):
   ws.send("hello")
   ws.send("testing")
   return

def connection():
   enableTrace(True)
   ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
   ws.on_open = on_open

   ws.run_forever()
   return

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

master.mainloop()

And this is the error message I get in IDLE whenever I try to send a message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:\Users\User\Desktop\thing.py", line 14, in sendmsg
    ws.send(msg)
NameError: global name 'ws' is not defined

I think this has something to do with the thread, but I am not sure what is causing the problem. The websocket on_open function successfully sends the messages to the server using ws.send(), but the sendmsg function does not. All help is appreciated as I am completely new to threading.

That's because you do

def sendmsg():
    msg = entry.get()
    ws.send(msg)
    return

You need to instantiate the websocket first:

def sendmsg():
    ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
    msg = entry.get()
    ws.send(msg)
    return

It looks like you do create a ws instance later on, within connection() , you do ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close) . Just make sure you have done that before you use ws , because that is where it gets defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM