目前我正在使用 Python tkinter 构建一个 GUI,要求用户在文本框(txt3)中输入详细信息
如何验证文本框是否已输入。如果没有输入,应该 显示消息“请输入文本框”。如果输入的话会经过 SaveInDB()保存到数据库中。
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)
上面的代码对我不起作用..请帮忙
最佳答案
您可以检查该条目是否有任何值,如果没有,请使用 showinfo
显示弹出消息。如果您不想要弹出窗口,您可以简单地设置焦点,如 entry.focus()
或使用不同的颜色突出显示背景。您想要完成的任务的最小示例。
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()
弹出版本
焦点版本
背景颜色版本
关于Python 检查文本框是否未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720516/