tkinter应用程序可以使用httpServer吗?

时间:2018-11-06 19:29:22

标签: python tkinter httpserver basehttpserver

我目前拥有以下可用作演示的代码。设置代码以使用按钮创建Tkinter窗口。按下按钮后,它将在本地主机上启动HttpServer。我遇到的问题是GUI进入“冻结” /“停滞” /“崩溃”状态,我不明白为什么。我意识到Tkinter是在单个线程中运行的,但是不确定单个线程会如何影响这一点。

def OpenUrl():
    run()

class server_callback(BaseHTTPRequestHandler):

  def do_GET(self):
        # Send response status code
        self.send_response(200)
        # Send headers
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send message back to client
        message = "You can now close this Browser session!"
        # Write content as utf-8 data
        self.wfile.write(bytes(message, "utf8"))
        return

def run():
  print('starting server...')
  server_address = ('127.0.0.1', 8080)
  httpd = HTTPServer(server_address, server_callback)
  print('running server...')
  httpd.serve_forever()

root = Tk()
frame = Frame(root)
frame.pack()
button = Button(frame, text="CLICK", command=OpenUrl)
button.pack()
root.mainloop()

0 个答案:

没有答案