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
Possible Duplicate:
Socket Thread and PyGTK
i want to build a GUI chat application.. I want to create a thread which able to accept the requested connection simultaneously. and show a message box to confirm requested connection to be accepted or rejected. The message box haven't appearing when the application running , it shows when the application close.. I am really getting confused.
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()
This is the class for crating socket and listening on it...
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gtk 不是线程安全的,它只是“线程感知”(在 Windows 中本质上是“线程无能力”)。任何时候只有一个线程可以访问 Gtk 对象。
在主程序中,在调用任何其他 gtk 函数之前,首先需要调用 gtk.gdk.threads_init()。然后,在你的线程中,任何时候你想要访问任何gtk对象,你必须首先调用gtk.gdk.threads_enter(),然后在访问它们之后,调用gtk.gdk.threads_leave()。
Gtk is not thread safe, it is merely "thread aware" (and essentially "thread incapable" in Windows). Only one thread can access a Gtk object at any time.
In your main program, you first need to call gtk.gdk.threads_init() before you call any other gtk functions. Then, in your thread, any time you want to access any gtk objects, you must first call gtk.gdk.threads_enter() and then after you access them, call gtk.gdk.threads_leave().