Python检查textbox是否未填充

2024-04-24 12:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在使用pythonttkinter构建一个GUI,它要求用户在textbox(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)

上面的代码不适用于我…请帮助


Tags: totextplaceguireswindowwidthtk
1条回答
网友
1楼 · 发布于 2024-04-24 12:19:52

您可以检查条目是否有任何值,如果没有,则使用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

相关问题 更多 >