首页 > 解决方案 > Tkinter 错误:AttributeError:“str”对象没有属性“items”

问题描述

我正在创建一个tkinter窗口,用于检查是否传递了正确的字符串,但如果我输入了错误的字符串,则会收到此错误:

AttributeError: 'str' object has no attribute 'items'

这是我的代码:

from tkinter import *

root = Tk()

e = Entry(root)
e.grid(row=0, column=0)

def buttonclick():
    if e.get() == "12345":
        goodLabel = Label(root, text="Good!")
        goodLabel.grid(row=3, column=0)
    else:
        badLabel = Label(root, "Bad!")
        badLabel.grid(row=3, column=0)

button = Button(root, text="Submit", command=buttonclick)
button.grid(row=2, column=0)

标签: pythonuser-interfacetkinter

解决方案


尝试这个

from tkinter import *

root = Tk()

e = Entry(root)
e.grid(row=0, column=0)

def buttonclick():
    if e.get() == "12345":
        goodLabel = Label(root, text="Good!")
        goodLabel.grid(row=3, column=0)
    else:
        badLabel = Label(root, text="Bad!")
        badLabel.grid(row=3, column=0)

button = Button(root, text="Submit", command=buttonclick)
button.grid(row=2, column=0)
root.mainloop()

只需使用text="Bad!"
这是您要找的东西吗?


推荐阅读