首页 > 解决方案 > 我不知道如何解决这个属性错误: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'

标签: pythontkinterattributeerrortkinter-entry

解决方案


推荐阅读