【发布时间】:2023-03-03 00:06:01
【问题描述】:
谁能告诉我我做错了什么?!我正在尝试在 Python 3 中使用 tkinter 创建 GUI。如果用户单击按钮并且同时 Entry 为空,我想显示 messageBox。 下面是我使用的代码。
知道我有这个错误:
if len(self.entryBox.get()) == 0:
AttributeError: 'NoneType' object has no attribute 'get'
如果self.entryBox is None:,我也尝试使用它,但在这种情况下,我在运行项目后立即看到 messageBox,现在是正确的。我真的很困惑。
代码:
from tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.widgets()
def widgets(self):
self.entryBox = Entry(self).grid()
Button(self, text="Submit", command=self.search()).grid()
def search(self):
if len(self.entryBox.get()) == 0:
tkinter.messagebox.showinfo("Warning!", "Box is empty! Write something")
else:
do_something()
# main
root = Tk()
root.title("Title")
app = Application(root)
root.mainloop()
【问题讨论】:
标签: python python-3.x object tkinter attributeerror