Reputation: 39
Currently I'm using Python tkinter to build a GUI that require user to enter the detail into the textbox(txt3)
How do I validate if the textbox is entered. If not entered, should show message "please enter textbox" . If entered, it will go through SaveInDB() to save in database.
def SaveInDB():
subID=(txt3.get())
if not (subID is None):
...my code here to save to db
else:
res = "Please enter textbox"
message.configure(text= res)`
txt3 = tk.Entry(window,width=20)
txt3.place(x=1100, y=200)
saveBtn = tk.Button(window, text="Save", command=SaveInDB ,width=20 )
saveBtn .place(x=900, y=300)
This code above does not work for me..Please help
Upvotes: 2
Views: 632
Reputation: 8215
You can check if the entry has any value and if not use showinfo
to display a popup message. If you don't want a popup you can simply set the focus like entry.focus()
or highlight the background with a different color. A minimal example of what you are trying to accomplish.
import tkinter as tk
from tkinter.messagebox import showinfo
def onclick():
if entry.get().strip():
print("Done")
else:
showinfo("Window", "Please enter data!")
#or either of the two below
#entry.configure(highlightbackground="red")
#entry.focus()
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
tk.Button(root, text='Save', command=onclick).pack()
root.mainloop()
Pop up version
Focus version
Background color version
Upvotes: 2