【问题标题】:Tkinter "Label object is not callable"Tkinter“标签对象不可调用”
【发布时间】:2016-03-07 15:50:21
【问题描述】:

我正在尝试将 Tkinter 标签的文本设置为等于来自 Entry 对象的用户输入。

以下是相关代码:

def Return(event):
    global Password
    global Input
    global SecurityLabel

    Password = Input.get()

    Parse(Password)
    Variations(Password)
    Search(Password)
    Additions(Password)
    Deductions(Password)
    CheckForAdjacency(ConvertToCoordinates(Password))
    MinimumRequirements(Password)

    print("Your password security rating is: " + str(Security))
    SecurityLabel['text'] = Security 

#GUI
MainWindow = Tk()
MainWindow.title("Password Assessor")
MainWindow.geometry("500x100")

TopMenu = Menu(MainWindow)
MainWindow.config(menu = TopMenu)

subMenu = Menu(TopMenu)
TopMenu.add_cascade(label="file", menu=subMenu)
subMenu.add_command(label="boop")

Label = Label(MainWindow, text="Please enter a password:")
Label.pack(fill = 'x')

SecurityLabel = Label(MainWindow, text="")
SecurityLabel.pack(fill = 'x')

Input = Entry(MainWindow, show="*")
Input.pack(fill = 'x')
Input.focus_set 

MainWindow.bind('<Return>', Return)

MainWindow.mainloop()

当从名为 Input 的输入框中按下返回键时,我使用以下代码获取用户的输入:

Password = Input.get()

我尝试使用以下代码将名为 SecurityLabel 的标签的文本设置为等于整数“Security”:

SecurityLabel['text'] = Security 

SecurityLabel 使用以下代码初始化:

SecurityLabel = Label(MainWindow, text="")
SecurityLabel.pack(fill = 'x')

当我尝试运行代码时,我得到了这个错误:

Traceback (most recent call last):
  File "/Users/kosay.jabre/Desktop/Password Assessor/Password.py", line 242, in <module>
    SecurityLabel = Label(MainWindow, text="")
TypeError: 'Label' object is not callable

我不知道我做错了什么。我怎样才能使这项工作?请尽量使您的解释简单。

【问题讨论】:

  • 这是因为您将变量命名为Label 并尝试再次调用Label() 来创建SecurityLabel,但现在它只看到变量而不是类。

标签: python tkinter


【解决方案1】:

问题出在这里:

Label = Label(MainWindow, text="Please enter a password:")
Label.pack(fill = 'x')

将标签命名为“标签”会产生一些问题。将此标签的名称更改为“标签”以外的名称解决了错误并使其他标签正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多