python - 我不知道如何解决这个属性错误:AttributeError: 'str' object has no attribute 'grid'
问题描述
我正在尝试用tkinter
. 我已经尝试了很多方法来解决这个问题,但似乎没有任何效果。最奇怪的是我正在处理一个条目,而不是一个字符串。
我的代码:
from tkinter import *
root = Tk()
root.title("Budget Manager")
budget = 2300
# Creating an entry to add/remove to the budget
add_budget_entry = Entry(root, width = 50).get()
add_budget_entry.grid(row = 1, column = 1)
# Creating the function that's called when button "Add/remove from budget" is pressed.
def AddBudget():
global budget
budget += add_budget_entry
return budget
# Creating the label to display the current budget
budget_label = Label(root, text = budget, padx = 20, pady = 10)
budget_label.grid(row = 0, column = 0)
# Creating the button that will add the given amount to the budget.
add_budget_button = Button(root, text = "Add/remove from budget", command = AddBudget())
add_budget_button.grid(row = 2, column = 1)
root.mainloop()
这个错误似乎很容易解决,但我不知道是不是因为我是新手tkinter
。最让我困惑的是,错误是在我为条目创建网格的行产生的,而不是字符串。
这是完整的错误,因此您可以具体查看产生错误的位置:
line 9, in <module>
add_budget_entry.grid(row = 1, column = 1)
AttributeError: 'str' object has no attribute 'grid'
解决方案
推荐阅读
- mongodb - When is it better to pull all data and filter, or pull the data filtered
- c++ - 如何在没有循环的情况下在 C++ 中打印人类可读的文件大小
- android - 如何让andorid studio中的函数参数停留在代码格式化的单行中?
- python-3.x - 有没有办法在 pytest HTML 报告中创建部分?
- mysql - 在 MySQL Workbench 错误的“where 子句”中接收“未知列”Dense_Rank
- python - 我如何在没有硒的情况下在 python 中登录我的路由器
- java - 如何从选项卡式片段创建自定义警报对话框?
- r - 在 R 中使用 JJAS 平均值的泰勒图
- python - 如何使 Python requests.Response 对象像字节一样?
- unity3d - Unity 当设备不支持您的纹理大小时会发生什么?