Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在套接字上接收到一些输入后创建顶级_Python_Sockets_Tkinter - Fatal编程技术网

Python 在套接字上接收到一些输入后创建顶级

Python 在套接字上接收到一些输入后创建顶级,python,sockets,tkinter,Python,Sockets,Tkinter,我使用这个论坛有一段时间了,但第一次问问题。 我有一个tkinter应用程序的问题。这是一个简单的聊天服务器-客户端程序,其中的聊天功能类似于Skype。我有一个朋友列表,当我从列表中选择朋友时,单击聊天,新窗口toplevel打开。然后我可以给朋友发短信。 问题在另一边。如果聊天窗口在另一端打开,它可以正常工作,但如果没有打开,我将尝试通过调用创建新toplevel的函数来创建。在这里,程序冻结在朋友一边 def bChatPress(self, event=None): de

我使用这个论坛有一段时间了,但第一次问问题。 我有一个tkinter应用程序的问题。这是一个简单的聊天服务器-客户端程序,其中的聊天功能类似于Skype。我有一个朋友列表,当我从列表中选择朋友时,单击聊天,新窗口toplevel打开。然后我可以给朋友发短信。 问题在另一边。如果聊天窗口在另一端打开,它可以正常工作,但如果没有打开,我将尝试通过调用创建新toplevel的函数来创建。在这里,程序冻结在朋友一边

def bChatPress(self, event=None):
        def closeChatClient():
            chatClient.destroy()
            del self.chatClients[friend]

        if self.friend=='':
            index = self.lbFriends.curselection()
            if index:
                friend = self.lbFriends.get(index)
                if friend not in self.chatClients:
                    chatClient=ChatClient(self, self.user, friend)
                    chatClient.protocol("WM_DELETE_WINDOW", closeChatClient)
                    self.chatClients[friend] = chatClient
        else:
            chatClient=ChatClient(self, self.user, self.friend)
            chatClient.protocol("WM_DELETE_WINDOW", closeChatClient)
            self.chatClients[self.friend] = chatClient
            self.friend=''

def receiveMessage(self):
        def loop():
            print('threadstart')
            while self.loged:
                try:
                    message = self.socket.recv(1024).decode('ascii')
                    print(message)
                    if '{LOGIN}' in message:
                        threading.Thread(target=self.login).start()
                    elif '{LOGOUT}' in message:
                        pass
                    elif '{CONNECT}' in message:
                        self.connect(message.replace('{CONNECT}',''))
                    elif '{DISCONNECT}' in message:
                        self.disconnect()
                    elif '{ADD FRIEND}' in message:
                        self.populateFriendsList(message.replace('{ADD FRIEND}',''))
                    elif '{DELETE FRIEND}' in message:
                        self.populateFriendsList(message.replace('{DELETE FRIEND}',''))
                    elif '{USER CONNECT}' in message:
                        self.checkOnline()
                    elif '{USER DISCONNECT}' in message:
                        self.checkOnline()
                    elif '{CHECK ONLINE}' in message:
                        self.populateFriendsList(message.replace('{CHECK ONLINE}',''))
                    elif '{MESSAGE}' in message:
                        self.processMessage(message.replace('{MESSAGE}',''))
                except Exception as error:
                    pass

        threading.Thread(target=loop).start()
这是创建聊天窗口的功能。当我通过单击按钮创建窗口时,它工作正常,但当我从receiveMessage函数调用此函数时,它不工作这是侦听套接字的循环

以前有人有过这个问题吗?
提前感谢。

除了创建根窗口的线程外,您不能从任何线程调用tkinter函数或小部件方法

您能发布接收消息的代码吗?错误不是常见的错误,程序只是冻结,没有任何通知。但是创建toplevel的函数在主窗口的类中。@user3169214:它定义在哪里并不重要-它是从哪个线程调用的?它是从接收消息的线程调用的。你们想说,若我只是通过调用函数来运行循环,而不是通过线程来调用它,我就能创建顶级吗?@user3169214:不,我不是这么说的。我是说你不能从多个线程创建小部件。我不明白这个。。。主线程是mainloop。messageReceive中只有其他线程。