pygtk多线程聊天应用程序中的问题



可能重复:
套筒螺纹和PyGTK

我想建立一个GUI聊天应用程序。。我想创建一个线程,能够同时接受请求的连接。并显示消息框以确认所请求的连接被接受或拒绝。该消息框在应用程序运行时没有显示,而是在应用程序关闭时显示。。我真的很困惑。

if __name__ == "__main__":
w=gtk.Window(gtk.WINDOW_TOPLEVEL)
ChatSock=PrivateChatWindowContent.ChatSocket(w)
#This the thread i am calling from main
    t=Thread(target=ChatSock.ListenThread)
#t.setDaemon(1)
t.start()

这是一个装入套接字并监听它的类…

class ChatSocket():
def __init__(self,window):
self.window=window
self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:        
  self.sock.bind(('',30099))
except ValueError,e:
  print e
self.sock.listen(10)
return
def ListenThread(self):
while 1:
#print "while loop"
(self.new_sock,self.client_addr) = self.sock.accept()
            self.new_sock.settimeout(1)
self.CloseDialog = gtk.MessageDialog(self.window,                                                       gtk.DIALOG_DESTROY_WITH_PARENT,                                 gtk.MESSAGE_QUESTION,                                       gtk.BUTTONS_YES_NO,                                     "New chat request from IP: [SomeIP]nDo you want to accept?")
respons=self.CloseDialog.run()
if respons==gtk.RESPONSE_YES:
    print "connection accepted"
    self.CloseDialog.destroy()
    ChatWindowThread=PrivateChatWindowContent.ChatWindow
            (self.window,[client ip],[client name])
elif respons==gtk.RESPONSE_NO:
    print "connection rejected"
    self.CloseDialog.destroy()
    self.sock.close()
return

Gtk不是线程安全的,它只是"线程感知"的(在Windows中基本上是"无线程的"(。任何时候只有一个线程可以访问Gtk对象。

在主程序中,在调用任何其他gtk函数之前,首先需要调用gtk.gdk.threads_int((。然后,在您的线程中,任何时候您想要访问任何gtk对象,都必须首先调用gtk.gdk.threads_enter((,然后在访问它们之后,调用gtk.gdk.threads_leave((.

最新更新