Python在tkinter中发送websocket消息
Posted
技术标签:
【中文标题】Python在tkinter中发送websocket消息【英文标题】:Python send websocket message in tkinter 【发布时间】:2015-08-23 22:37:27 【问题描述】:我在 python 2.7 上的 tkinter 中发送 websocket 消息时遇到问题。我目前有 tkinter 和一个 websocket 客户端在一个线程中运行,因此它们两个循环同时工作,如 here 所示。问题是,每当我尝试输入消息并按下发送按钮时,都会收到 NameError。我使用的 websocket 客户端是here。
这是我的代码:
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()
这是我在尝试发送消息时在 IDLE 中收到的错误消息:
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
我认为这与线程有关,但我不确定是什么导致了问题。 websocket on_open 函数使用 ws.send() 成功将消息发送到服务器,但 sendmsg 函数没有。感谢所有帮助,因为我对线程完全陌生。
【问题讨论】:
【参考方案1】:那是因为你这样做
def sendmsg():
msg = entry.get()
ws.send(msg)
return
你需要先实例化websocket:
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
看起来你稍后确实创建了一个 ws 实例,在 connection()
中,你创建了 ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
。请确保在使用 ws
之前已完成此操作,因为这是定义它的地方。
【讨论】:
我认为这不是问题所在。 websocket是在connection函数中发起的,这个函数是用线程启动的。除非使用 sendmsg 以某种方式无法识别,这是我需要解决的问题。 尝试将ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
添加到您的sendmsg()
函数中。我将更新答案以向您展示我的意思...
我已将其更新为您发布的内容,现在显示为 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 15, in sendmsg ws.send(msg) File "C:\Python27\lib\site-packages\websocket_client-0.32.0-py2.7.egg\websocket\_app.py", line 105, in send raise WebSocketConnectionClosedException("Connection is already closed.") WebSocketConnectionClosedException: Connection is already closed.
以上是关于Python在tkinter中发送websocket消息的主要内容,如果未能解决你的问题,请参考以下文章
python tkinter如何设置组件在窗口中的位置,比如说一个按钮,我希望这个按钮在窗口的左边,上边………