将 Tkinter 与 Tornado Websockets 结合使用。我怎样才能同时获得 运行?

Use Tkinter with Tornado Websockets. How can I get both running?

我想创建一个简单的 python 应用程序,它在 TKinter 中显示 Window 它从 Websocket 接收的内容。

我的问题是,我无法同时获得两个 运行ning。对于通信我会使用队列,但我之前卡住了一步。

我的计划是 运行 在一个额外的线程中循环龙卷风,但不幸的是它不起作用。我搜索解决方案,但没有找到任何有用的东西。

这是我的测试应用程序:

import threading
import tornado.ioloop
import tornado.web
import tornado.websocket
import Tkinter

class TornadoThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        application = tornado.web.Application([(r'/',WSHandler)])
        application.listen(9090)
    def run(self):
        print "Start a tornado"
        tornado.ioloop.IOLoop.instance().start()

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'connection opened...'
        self.write_message("The server says: 'Hello'. Connection was accepted.")

    def on_message(self, message):
        #self.write_message("The server says: " + message + " back at you")
        print 'received:', message

    def on_close(self):
        print 'connection closed...'



TornadoThread = TornadoThread()

# Start new Threads
TornadoThread.start()

top = Tkinter.Tk()
top.mainloop()

`

您在调用 IOLoop.instance().start() 时缺少括号。修复了这个问题(以及其他一些更改,修复了 运行 方法的缩进并将 myThread() 更改为 TornadoThread()),您的代码对我有用。