python - 如何修复 - 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
我试过的:
我尝试在谷歌上搜索答案,但我不能说我没有得到任何答案,但我不明白如何为我的问题实施这些解决方案。这些是我提到的链接
解决方案
你的问题是这条线
set.gui_done = False
set
是python中的受保护关键字。set
它是为数据类型保留的。通过这一行,您告诉解释器将gui_done
内置set
类型的属性更改为 false,这是不允许的。这就是为什么你得到一个TypeError
. 如果它不是内置类型,您仍然会得到一个AttributeError
因为set
没有gui_done
属性。您可能打算使用self
而不是set
推荐阅读
- javascript - 如何使用 Swift 和 JavaScript 在 WKWebView 中滚动文本
- typescript - 如何计算打字稿中对象的动态键类型
- xcode - xcode 12中的lldb printf
- android - Android MVVM ROOM 单一事实来源问题
- sql - SQL 在新计算中使用别名
- javascript - 为什么 defaultOpen 可以在我的代码模拟器上运行,但不能在我的网站上运行?
- javascript - UseState 未使用 TextInput 正确更新 | 反应
- react-native - 不透明度褪色动画不适用于 react-native-reanimated 2
- r - R中的动态GMM
- validation - 从电子邮件的用户名部分(不是域)查找一次性电子邮件地址