如何同时启动Python Gui和Websocket?

时间:2016-07-21 09:21:44

标签: python multithreading tkinter

我正在尝试导入python包tKinter,并且无法在循环上打开GUI窗口,并在另一个循环上侦听websocket连接。

我试过线程 线程1是新的套接字连接 线程2是打开的窗口

我想我忘了在其中一个线程上调用sleep而不显示到控制台

这是我需要同时运行的主要功能

if __name__ == "__main__":
   """
   __main__: starts a server from SimpleWebSocketServer and creates class
   SimpleEcho

   prints messages
   """
   app = Tk()
   app.title('GUI')
   app.mainloop()

   app.wm_protocol("wm_WINDOWCLOSED", sys.exit(1)


   parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
   parser.add_option("--host", default='', type='string', action="store", dest="host", help="hostname (localhost)")
   parser.add_option("--port", default=8000, type='int', action="store", dest="port", help="port (8000)")
   parser.add_option("--example", default='echo', type='string', action="store", dest="example", help="echo, chat")
   parser.add_option("--ssl", default=0, type='int', action="store", dest="ssl", help="ssl (1: on, 0: off (default))")
   parser.add_option("--cert", default='./cert.pem', type='string', action="store", dest="cert", help="cert (./cert.pem)")
   parser.add_option("--ver", default=ssl.PROTOCOL_TLSv1, type=int, action="store", dest="ver", help="ssl version")

   (options, args) = parser.parse_args()
   client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
      client_socket.connect(("localhost", 2500))
   except socket.error:
      print "Could not connect to the main frame for TUIO transfer, try again"
      sys.exit(1)
   cls = SimpleEcho                                                        #echos the websocket calls to client_socket

   server = SimpleWebSocketServer(options.host, options.port, cls)         #create new server on websock


   def close_sig_handler(signal, frame):
      server.close()
      sys.exit()

   signal.signal(signal.SIGINT, close_sig_handler)

   server.serveforever()

当我运行该程序时,窗口将打开,不会让websocket连接开始。

0 个答案:

没有答案