Tkinter没有启动窗口

时间:2016-03-22 20:42:42

标签: python multithreading sockets tkinter

我正在用Python创建一个基本的聊天客户端,但它不会打开窗口。它似乎在服务器线程启动时冻结。我正在使用socketserver模块。

class ChatClient(Frame):
    def CreateWidgets(self):
        self.whoislist = Label(self,text = '')
        def updateWhoIs(peopleList):
            finishlist = 'People:'
            for person in peopleList:
                finishlist += ('\n' + person)
            self.whoislist.config(text = finishlist)
        self.whoislist.grid(row = 1, columnspan = 7)
        self.mainchat = Text(self)
        self.mainchat.grid(columnspan = 5, rowspan = 3, row = 2, column = 3)
    peeps = []
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.grid()
        self.CreateWidgets()
        print('Huh.')
        self.server = socketserver.TCPServer(('127.0.0.1',1337),MyTCPHandler)
        self.sthread = Thread(target = self.server.serve_forever())
        self.sthread.start()
        print('Yay')

1 个答案:

答案 0 :(得分:1)

在初始化线程时调用self.server.serve_forever(),将其返回值设置为线程的目标,但顾名思义:它会持续存在。而是将方法(不调用它)指定为目标:

self.sthread = Thread(target = self.server.serve_forever)

并且它应该工作,但要小心MyTCPHandler如何与Tk应用程序交互,因为tkinter不是线程安全的。