有人能帮我使用Python GTK+套接字吗?我正在尝试聊天,所以我运行这个程序来接收来自其他用户的消息:
def socket_recv(socket, buffer):
while True:
try:
message = socket.recv(1024).decode()
end_iter = buffer.get_end_iter()
buffer.insert(end_iter, message + "n")
while Gtk.events_pending():
Gtk.main_iteration()
except:
print("Você foi desconectado do servidor")
socket.close()
break
总的来说,我正在尝试运行这个事件:
def onConnect(self, widget):
self.nickname = self.nickname_entry.get_text().strip()
self.adress = self.adress_entry.get_text().strip()
if self.nickname == "" or self.adress == "":
self.error_message.show()
return
HOST, PORT = self.adress.split(':')
PORT = int(PORT)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((HOST, PORT))
self.sock.send(str.encode(self.nickname))
self.builder.add_from_file("interface.glade")
self.builder.connect_signals(self)
self.chat_text.set_editable(False)
self.chat_text.set_wrap_mode(3)
self.chat_buffer = self.chat_text.get_buffer()
self.chat_buffer.set_text("Início do chatn")
self.end_iter = self.chat_buffer.get_end_iter()
self.chatWindow.show()
while Gtk.events_pending():
Gtk.main_iteration()
thread = threading.Thread(target=socket_recv(self.sock, self.chat_buffer))
thread.start()
我在GUI的启动窗口中获得用户的地址和昵称,然后在他们点击按钮"后尝试连接到服务器;"连接";,然后出现聊天窗口。虽然我可以连接到服务器,但当我尝试使用socket_recv运行聊天GUI时,GUI会变得完全不起作用(按钮无法按下等(,我不得不强制退出以关闭应用程序。我对Python、GTK和线程还很陌生,如果能为提供任何帮助,我将不胜感激
问题解决了,伙计们,我刚刚更改了
threading.Thread(target=socket_recv(self.sock, self.chat_buffer))
至
thread = threading.Thread(target=socket_recv, args=(self.sock, self.chat_buffer))
很抱歉给您带来不便