python - 使用 grid() 函数的“AttributeError”
问题描述
使用 gride 时出现错误:“AttributeError:'NoneType' 对象没有属性 'grid'”。
这是我的代码:
root = tk.Tk()
root.configure(bg="black")
# Creates labels
tk.Label(root, image=logo1).pack()
# Creates buttons
tk.Button(root, image=logo2, command=root.destroy).pack().grid(row=0, column=0)
tk.Button(root, image=logo3, command=root.destroy).pack().grid(row=0, column=0)
root.mainloop()
解决方案
您同时使用了两个包装管理器,但您使用错误。
打包调用将返回 None,并且您正在尝试将网格调用分配给该 None。
这样做:
tk.Button(root, image=logo2, command=root.destroy).grid(row=0, column=0)
tk.Button(root, image=logo3, command=root.destroy).grid(row=0, column=0)
或者使用包管理器而不是网格:
tk.Button(root, image=logo2, command=root.destroy).pack()
tk.Button(root, image=logo3, command=root.destroy).pack()
推荐阅读
- msbuild - TFS Build 2013 避免代码分析
- powershell - foreach 的输入和输出到表中的列
- reactjs - 区块链交易中的 Chrome 扩展问题
- .net - .NET 的缓存实现
- java - Mockito 无法使用谓词函数
- mysql - 从两列中选择值并将它们连接到不同表的列中
- json - 如何发布两个文件
- android - 动态复选框上的 onclicklistener 并检查复选框是否被选中
- javascript - 未捕获的类型错误:无法读取 app.js:4 处未定义的属性“addEventListener”
- function - 仅使用一个应用 lambda 的方法接口改进 Kotlin 扩展函数的语法