类python之间的双向通信(编辑+代码添加)

时间:2017-04-19 15:09:16

标签: python oop logic

所以,我的课程需要相互沟通。例如,我构建了聊天程序的客户端。一个类处理套接字,另一个类处理GUI。因此,GUI类必须从套接字类中获取接收的数据,此外还必须通过套接字类发送消息。

问题是如果你让一个类包含另一个你不能双向沟通。您需要将父项的实例传递给子项但我不认为传递实例是正确的方法。

最好的做法是什么?

这里以一些代码为例,希望其清楚(请阅读代码中的注释以便更好地理解):

class MainWindow:
    def __init__(self, master, username, sock):
        # Tkinter Code Here

    def insert_msg(self, data):  # d_type 1 - msg
        self.chat_textbox.insert(END, "\n%s %s" % (datetime.now().strftime('%H:%M:%S'), data))
        self.chat_textbox.see(END)

    def send_msg(self, data, d_type=SEND_ENUM.TYPE_MSG, arg=0):
        data = str(data)
        if len(data) >= 1 and d_type == SEND_ENUM.TYPE_MSG:
            try:
                self.chat_textbox.insert(END, "\n%s [Me] %s" % (datetime.now().strftime('%H:%M:%S'), data))

                # For example, here has to use send_msg method from SockHandler class
                # self.sock_handler.send_msg(data)

            except sock_handling.ConnectionError as error:
                self.chat_textbox.insert(END, "\nError: The message was not delivered", "RED")
            else:
                pass
            finally:
                self.msg_box_entry.delete(0, 'end')
                self.chat_textbox.see(END)
        elif d_type != SEND_ENUM.TYPE_MSG:
            try:
                # also here self.sock_handler.send_msg(data, d_type)
            except sock_handling.ConnectionError as error:
                pass
        else:
            pass

class SockHandler:
    def __init__(self, client_socket):
        # nothing relevant for the Q

    def send_msg(self, data, d_type=SEND_ENUM.TYPE_MSG, arg=0):
        packed_data = self.pack_data(d_type, arg, data)

        if len(data) >= 1 and d_type == SEND_ENUM.TYPE_MSG:
            try:
                self.client_socket.send(packed_data)
            except socket.error:
                raise ConnectionError("Connection Error")

            finally:
                pass

        elif d_type != SEND_ENUM.TYPE_MSG:
            try:
                self.client_socket.send(packed_data)
            except socket.error:
                raise ConnectionError("Connection Error")

    def receive_data(self):
        try:
            while True:
                recv_data = self.client_socket.recv(self.BUFFER)
                (d_type,), data = struct.unpack("!I", recv_data[:4]), recv_data[4:]
                if d_type == RECV_ENUM.TYPE_MSG:

                    # For example, here has to use insert_msg method from MainWindow class

                elif d_type == RECV_ENUM.TYPE_USER_LIST:
                    pass
                elif d_type == RECV_ENUM.TYPE_POKE:
                    pass
        except socket.error:
            self.client_socket.close()
            raise ConnectionError("Connection Error")

1 个答案:

答案 0 :(得分:0)

为什么不将SockHandler实例化为MainWindow

的属性

像这样:

class MainWindow:
    def __init__(self, master, username, sock):
        # Tkinter Code Here
        self.socket_handler = SockHandler(sock)

现在,您可以使用MainWindow

self.socket_handler.send_msg发送消息

对于接收,让您的套接字存储接收到MainWindow可以检索它的数据。

像这样:

class SockHandler:
    def __init__(self, client_socket):
        # nothing relevant for the Q
        self.received_messages = Queue.Queue()

现在,您需要为MainWindow提供一种方式来访问收到的消息。如果你愿意,你可以通过查询队列直接获取消息,但我更喜欢这样的东西:

# Within the SockHandler Class
def get_next_message():
    try:
        return self.received_messages.get(True, 5)
    except:
        return None

如果SockHandlerMainWindow处理此方法中的超时异常,则无关紧要。这取决于你。