首页 > 解决方案 > 如何修复 - TypeError:无法设置内置/扩展类型“set”的属性

问题描述

我对 python 比较陌生,我正在尝试用 python 进行简单的 GUI 聊天。它被编程为在客户端加入服务器时询问昵称。一切正常,直到我输入昵称的部分。当我输入昵称时,我分别从服务器和客户端收到这些错误,我也会提供回溯。

ConnectionResetError: [Errno 104] Connection reset by peer (来自服务器)

追溯:

Traceback (most recent call last):
  File "server.py", line 51, in <module>
    receive()
  File "server.py", line 44, in receive
    broadcast(f"{nickname} entered to the chat!\n".encode('utf-8'))
  File "server.py", line 17, in broadcast
    client.send(message)

TypeError: can't set attributes of built-in/extension type 'set' (来自客户)

追溯:

Traceback (most recent call last):
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 94, in <module>
    client = Client(HOST, PORT)
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 22, in __init__
    set.gui_done = False

服务器和客户端的代码也是链接的。

服务器-> https://pastebin.com/0W7Cw9Cu

客户端-> https://pastebin.com/FES2UNc1

我试过的:

我尝试在谷歌上搜索答案,但我不能说我没有得到任何答案,但我不明白如何为我的问题实施这些解决方案。这些是我提到的链接

  1. Python 处理 socket.error: [Errno 104] Connection reset by peer

  2. python无法设置内置/扩展类型'object'的属性

标签: python

解决方案


你的问题是这条线

set.gui_done = False

set是python中的受保护关键字。set它是为数据类型保留的。通过这一行,您告诉解释器将gui_done内置set类型的属性更改为 false,这是不允许的。这就是为什么你得到一个TypeError. 如果它不是内置类型,您仍然会得到一个AttributeError因为set没有gui_done属性。您可能打算使用self而不是set


推荐阅读