python - 这个“自我未定义”错误是范围问题吗?
问题描述
在我的主脚本中,我有以下代码:
class Sequence:
def __init__(self, colour, text):
self.colour = colour
self.width = 5
self.height = 5
self.text = text
def create_window(self, root):
self.name=tk.Label(root,text=self.text, wdith=self.width, height=self.height,
bg=self.colour
self.name.pack()
在我的 gui 脚本中,此代码运行为
Sequence.create_window(self, root)
我得到错误:
Sequence.create_window(self, root) NameError: name 'self' is not defined
我不确定如何解决这个问题,它与程序的范围有关吗?
解决方案
您需要先初始化一个对象:
class Sequence:
def __init__(self, colour, text):
self.colour = colour
self.width = 5
self.height = 5
self.text = text
def create_window(self, root):
self.name=tk.Label(root,text=self.text, wdith=self.width, height=self.height,
bg=self.colour)
self.name.pack()
sequence = Sequence("blue", "test")
sequence.create_window(root)
推荐阅读
- reactjs - 如何复制反应组件以进行错误记录?
- php - 将上传的图像存储到数据库 laravel 8
- python - 远程服务器上的文件存在检查器总是返回 255
- php - PHP/WordPress 解析错误,意外的“?”
- javascript - 使用 Angular 选择除禁用复选框之外的所有复选框检查
- javascript - 如何使用 react-music-player 组件播放本地 mp3 或 midi 文件
- c# - 如何将 PropertyInfo[] 转换为我的班级
- iis - 处理 IIS 中的授权标头
- python - Selenium Can't find "HOME" element on Facebook
- sql - 测试使用 SQL 连接池和提交的 Node JS 应用程序