我现在正试图让我的 PC 上的 GUI 与每个套接字的服务器通信。
这是GUI的部分代码:
def listenToServer(self):
""" keep listening to the server until receiving 'All Contracts Finished' """
self.feedbackWindow.appendPlainText('--Executing the Contracts, Listening to Server--')
contentsListend = ''
while contentsListend != 'All Contracts Finished':
#keep listen from the socket
contentsListend = self.skt.recv(1024)
#make the GUI show the text
self.feedbackWindow.appendPlainText(contentsListend)
另一方面,服务器将一个接一个地发送数据,但有一定的间隔。这是模拟服务器的测试代码:
for i in range(7):
print 'send back msg, round: ', i # this will be printed on the screen of the server, to let me know that the server works
time.sleep(1) # make some interval
# c is the connected socket, which can send messages
# just send the current loop number
c.send('send back msg' + str(i))
c.send('All Contracts Finished')
c.close()# Close the connection
现在,除了在服务器中的整个 for 循环之后,GUI 只会显示接收到的消息的问题之外,一切正常。一旦我运行服务器和 GUI。服务器端以正确的速度将消息一一打印到屏幕上,但 GUI 没有响应,它不更新。直到程序结束,所有 7 行都在 GUI 端同时出现。我希望它们一一出现,以便稍后我可以在我的 PC 上使用此 GUI 检查服务器的状态。
谁能帮忙,非常感谢!