python - 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)
解决方案
尝试这个
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!"
这是您要找的东西吗?
推荐阅读
- javascript - 我在网页上的 mp4 背景并不总是开始播放
- docker - 来自 /172.18.0.1 的意外错误;关闭连接(org.apache.kafka.common.network.Selector)
- logging - JMeter Logger (jmeter.log) - 如何更改时间戳格式 (jmeter 5.2.1)
- html - 打印时向上或向下浮动 html 元素以避免空白
- c++ - Is it worth making my code less readable for providing exception safety in case of out of memory errors?
- javascript - MongoDB - JavaScript:Web 应用程序停止工作,因为 MongoDB 已满?
- android - Android notification's names on settings like facebook
- php - htaccess 将请求重定向到 index.php,以前的路径作为查询字符串
- javascript - 关于使用猫鼬或仅 JS 过滤器进行过滤的问题
- excel - Power Query 中的拆分行