Python 检查文本框是否未填充

标签 python tkinter

目前我正在使用 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()

弹出版本

enter image description here

焦点版本

enter image description here

背景颜色版本

enter image description here

关于Python 检查文本框是否未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720516/

相关文章:

python - get() 在标记为唯一的字段上排除 MultipleObjectsReturned

Python tkinter 标签

python - 单击按钮时如何更改日期?

python - 使用 "wait_variable()"时无法退出 tkinter 应用程序

PYTHON:如何使用 tkinter 将两个标签锚定到顶部

python - 按下时创建一个按钮在python 3.3的输入框中打印该按钮上的数字

python - 如何将 (*args,**kwargs) 类型的属性传递给类内的函数?

python - 在 Python 中从 datetime.now() 中减去 SQL DATETIME

python - C++ Python 嵌入 : Run on machine with no Python?

python - 替换 Python 记录器的默认处理程序