2015-08-23 130 views
0

我無法在python 2.7中的tkinter中發送websocket消息。我目前有一個tkinter和一個websocket客戶端運行在一個線程中,所以他們兩個循環同時工作,見here。問題是,每當我嘗試輸入消息並按下發送按鈕,我就會得到一個NameError。我正在使用的websocket客戶端是herePython在tkinter中發送websocket消息

這是我的代碼:

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() 

這是錯誤消息我在空閒狀態,每當我試圖發送一個消息:

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函數不會。所有的幫助表示讚賞,因爲我對線程完全陌生。

回答

0

那是因爲你做

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之前,請確保你已經完成了這個工作,因爲那是它被定義的地方。

+1

雖然我不認爲這是問題。 websocket由連接函數啓動,該函數以線程啓動。除非使用sendmsg以某種方式不承認,這是我需要修復的。 –

+0

嘗試在sendmsg()函數中添加'ws = WebSocketApp(「ws://echo.websocket.org/」,on_message = on_message,on_error = on_error,on_close = on_close)''。 – rofls

+0

我將它更新爲你發佈的內容,現在它說'Tkinter回調中的異常 回溯(最近調用最後一次): 文件「C:\ Python27 \ lib \ lib-tk \ Tkinter.py「,第1486行,在__call__中 return self.func(* args) sendmsg文件」C:\ Users \ User \ Desktop \ thing.py「,第15行 ws.send (msg) 文件「C:\ Python27 \ lib \ site-packages \ websocket_client-0.32.0-py2.7.egg \ websocket \ _app.py」,第105行,發送 raise WebSocketConnectionClosedException(「Connection is already closed 。「) WebSocketConnectionClosedException:連接已關閉。 –