【问题标题】:Error "'function' object has no attribute 'tk'" [duplicate]错误“'function'对象没有属性'tk'” [重复]
【发布时间】:2021-03-01 18:51:04
【问题描述】:

所以我在窗口内创建一个标签,我得到了错误

'function'对象没有属性'tk'

我不明白发生了什么,因为我在这段代码中有选项菜单,而且没有标签似乎可以正常工作。

函数如下:

def taxesFrame():
    global taxesWindow
    global lowTaxVar
    global middleTaxVar
    global highTaxVar
    taxesWindow = Tk()
    taxesWindow.title('Taxes')
    taxesWindow.state('zoomed')

    taxesTitle = Label(taxesFrame, text = "Taxes")
    taxesTitle.configure(font=(titlefont))
    taxesTitle.pack()

    taxesDescription = Label(taxesFrame, text = "Set Your Taxes")
    taxesDescription.configure(font=(subtitlefont))
    taxesDescription.pack()

    lowTaxVar = StringVar(taxesWindow)
    lowTaxVar.set("Select Taxes for the Lower Class")
    lowTaxRate = OptionMenu(taxesWindow, lowTaxVar, *taxesList)
    lowTaxRate.pack()

    middleTaxVar = StringVar(taxesWindow)
    middleTaxVar.set("Select Taxes for the Middle Class")
    middleTaxRate = OptionMenu(taxesWindow, middleTaxVar, *taxesList)
    middleTaxRate.pack()

    highTaxVar = StringVar(taxesWindow)
    highTaxVar.set("Select Taxes for the Upper Class")
    highTaxRate = OptionMenu(taxesWindow, highTaxVar, *taxesList)
    highTaxRate.pack()
    return lowTaxVar, middleTaxVar, highTaxVar

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    每个 tkinter 小部件中的第一个位置参数应该是 master,但在这里你传入了函数名 (taxesFrame),因此会出现错误:

    taxesTitle = Label(taxesWindow, text = "Taxes") # Change to taxesWindow
    taxesDescription = Label(taxesWindow, text = "Set Your Taxes")
    

    假设您的意思是 taxesWindowmasterLabel

    【讨论】:

    • 所以我将标签设置为函数而不是窗口...谢谢酷云
    猜你喜欢
    • 2018-02-18
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2019-11-13
    • 2019-05-30
    相关资源
    最近更新 更多