python 2.7线程GUI和套接字

时间:2016-03-08 18:09:19

标签: python multithreading python-2.7 sockets tkinter

最近我一直在研究我的python聊天项目,服务器运行良好,但是当我使用线程模型来运行客户端的gui时,选择我遇到了问题。

我能够将数据发送到服务器,服务器将数据返回给客户端,但客户端无法打印数据。

这是客户的代码:

import socket
import select
from Tkinter import *
import thread

class client(object):

    def __init__(self):
        self.client_socket = socket.socket()
        self.messages =[]

    def connect(self):
        self.client_socket.connect(('127.0.0.1', 1234))

    def getgui(self, window):
        self.root = window


    def run(self):
        while True:
            self.rlist, self.wlist, self.xlist = select.select([self.client_socket], [self.client_socket], [])
            for current_socket in self.rlist:
                data = current_socket.recv(1024)
                self.root.print_on_list(data)

    def send_to_server(self, word):
        self.client_socket.send(word)




class rootgui():

    def __init__(self,client_socket):
        self.client_socket = client_socket
        self.root = Tk()
        self.root.geometry('600x700')
        self.root.minsize(600, 700)
        self.root.maxsize(600, 700)
        self.root.title('chat')

        self.frame = Frame(self.root)
        self.frame.pack()


        self.scrollbar = Scrollbar(self.frame)
        self.scrollbar.pack(side = RIGHT, fill = Y)

        self.chatlist = Listbox(self.frame, yscrollcommand = self.scrollbar.set, width = 80, height = 25 )
        self.chatlist.pack(side = LEFT)

        self.leftframe = Frame(self.root)
        self.leftframe.pack(side = LEFT)

        self.send_button = Button(self.leftframe, text = 'send', bg = 'green', padx = 60, pady = 70, command = self.send)
        self.send_button.pack()

        self.rightframe = Frame(self.root)
        self.rightframe.pack(side = RIGHT)

        self.text = StringVar()
        self.input_box = Entry(self.rightframe, width =55, textvariable = self.text )
        self.input_box.pack()

        self.scrollbar.config(command = self.chatlist.yview)


    def mainloop(self):
        self.root.mainloop()

    def send(self):
        t = self.text.get()
        self.client_socket.send_to_server(t)
        return

    def print_on_list(self, data):
        self.chatlist.insert(END, data+'\r\n')



def main():
    client_socket = client()
    client_socket.connect()
    window = rootgui(client_socket)
    client_socket.getgui(window)
    thread.start_new_thread(window.mainloop())
    thread.start_new_thread(client_socket.run())



if __name__ == '__main__':
    main()

这是服务器的代码:

import socket
import select

class server(object):

    ''' init function '''
    def __init__(self):
        self.server_socket = socket.socket()
        self.open_client_sockets = []

    '''this function send a message to all of the connected clients'''
    def send_messages(self, message):
        for client_socket in self.open_client_sockets:
            client_socket.send(message)

    ''' this function get a port , it bind the server socket and set the listen to 5 sockets '''
    def connection(self):
        self.server_socket.bind(('0.0.0.0', 1234))
        self.server_socket.listen(5)

    ''' this function use the select library and read / write from / to the client socket '''
    def run(self):
        while True:
            self.rlist, self.wlist, self.xlist = select.select([self.server_socket]+self.open_client_sockets, self.open_client_sockets, [])
            for current_socket in self.rlist:
                if current_socket is self.server_socket:
                    (new_socket, address) = self.server_socket.accept()
                    self.open_client_sockets.append(new_socket)
                    print 'new member : ' + str(address)
                else:
                     try:
                        data = current_socket.recv(1024)
                        self.send_messages(data)
                        break
                     except socket.error:
                        self.open_client_sockets.remove(current_socket)
                        print 'Connection with client closed.'


if __name__ == '__main__':
    server_socket = server()
    server_socket.connection()
    server_socket.run()

0 个答案:

没有答案