Could someone tell me what`s wrong I did?! I am tring to create GUI with tkinter in Python 3. If user click the button and in the same time Entry is empty I want to show messageBox. Here below my code that I used.
Right know I have this ERROR:
if len(self.entryBox.get()) == 0:
AttributeError: 'NoneType' object has no attribute 'get'
Also I tried to use this if self.entryBox is None:
but in that case I see messageBox immediately after run the project which is now correct. I am really comfused.
CODE:
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()
The line self.entryBox = Entry(self).grid()
will assign the result of .grid()
to self.entryBox
. Intead, try this:
self.entryBox = Entry(self)
self.entryBox.grid()
By doing Button(self, text="Submit", command=self.search()).grid()
you have a similar problem, as you run the search
method once and then bind the result of that method to the command
parameter. Instead, this should work (note the absence of ()
:
Button(self, text="Submit", command=self.search).grid()