在 tkinter 中使用 python websocket 客户端 [英] Using python websocket client with tkinter

查看:59
本文介绍了在 tkinter 中使用 python websocket 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 2.7 中使用 websocket 客户端.客户端在 IDLE 运行时工作正常,如收到的消息所示.但是,尝试在 tkinter 文本小部件中插入 websocket 消息不起作用.消息仍在 IDLE 中显示,但 tkinter 窗口根本没有显示.

I'm trying to make use a websocket client in python 2.7. The client works fine when running with IDLE, as in the received messages are shown. However trying to insert the websocket messages in a tkinter text widget is not working. Messages are still being displayed in IDLE, but the tkinter window is not being displayed at all.

我使用的包叫做 websocket-client 0.32.0,我从 这里下载了它.我按照有关如何制作类似 api 的 javascript 的说明进行操作,并对代码进行了一些修改.

The package I'm using is called websocket-client 0.32.0 and I dowloaded it from here. I followed the instructions on how to make a javascript like api and somewhat adapted the code.

这是我的代码:

from Tkinter import *
from websocket 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)

text = Text(master)
text.pack(expand=True,fill=BOTH)

def on_message(ws, message):
   text.insert(END, 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("hi")
   ws.send("test")
   return

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

master.mainloop()

如果这也有帮助,以下是 IDLE 中显示的内容:

Here is what is shown in IDLE if that is also helpful:

--- request header ---
GET / HTTP/1.1

Upgrade: websocket

Connection: Upgrade

Host: echo.websocket.org

Origin: http://echo.websocket.org

Sec-WebSocket-Key: tXWAVVlaRoq2S4+p/z12gg==

Sec-WebSocket-Version: 13




-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Fri, 21 Aug 2015 05:16:54 GMT
Sec-WebSocket-Accept: LH12LFLFaek6HgCnGIugF0sg9lA=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
send: '\x81\x82{b\x97\xfc\x13\x0b'
send: '\x81\x84\xa5J\xecf\xd1/\x9f\x12'
Received: hi
Received: test

我已经被困在这个问题上一段时间了,我找不到任何解决此类问题的方法.感谢任何帮助,因为我是新手.

I've been stuck on this for a while and I cannot find any solutions to this sort of problem. Any help is appreciated as I'm a newbie.

推荐答案

好吧,我不使用 websocket ,但问题似乎是,当你使用时 -

Well, I do not use websocket , but the issue seems to be that , when you do -

ws.run_forever()

应用程序进入无限循环,因此控制永远不会到达 master.mainloop() ,这是 tkinter 显示所必需的,这又是另一个无限循环,它只是当您关闭 gui 时退出.

The application goes into an infinite loop , so the control never reaches master.mainloop() , which is required for the tkinter to show up , and which is again another infinite loop, which is only exited when you close the gui.

你真正想要的是同时运行两个无限循环,这需要你在两个不同的线程中运行它们,目前它们都定义在同一个线程上,所以一个只会在另一个退出后运行.

What you really want is to run two infinite loop simultaneously, which would require you to run them in two different threads, currently both of them are defined on the same thread, so one would only run after the other has exited.

一个快速的解决方法是使用 master.after() 有一定的延迟并给出一个函数,然后在函数中为 websocket 连接到服务器的函数启动一个新线程.这样两个无限循环都在不同的线程中.

A quick fix would be to use master.after() with a certain delay and giving a function, and then in the function start a new thread for a function in which websocket connects to the server. so that both the infinite loops are in different threads.

但最好的解决方法是使用按钮,我们称之为 'Connect' .然后该按钮的回调将是一个类似 on_connect() 下面的函数,它启动连接函数 -

But the best fix would be to use a button, lets call it 'Connect' . Then the callback for that button would be a function like on_connect() below , which starts the function to connect -

def connect_to_socket():
    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()

def on_connect():
    import threading
    t = threading.Thread(target=connect_to_socket)
    t.start()

按钮类似于 -

button = Button(master, text="Connect", command=on_connect)

然后你可以随意放置按钮.

Then you can place the button as you wish.

这篇关于在 tkinter 中使用 python websocket 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆