如何检查 Tkinter 中是否有任何输入框为空? [英] How do I check if any entry boxes are empty in Tkinter?
问题描述
我正在使用 Python 2.7 在 Tkinter 中制作 GUI.我有一个框架,其中包含大约 30 个输入框.我现在无法访问代码,但有没有办法检查其中一个或几个框是否为空,以便我可以警告用户并询问他们是否要继续?
I am making a GUI in Tkinter with Python 2.7. I have a frame with about 30 entry boxes among other things. I don't have access to the code right now, but is there a way to check if any or several of the boxes are empty so I can warn the user and ask them if they want to proceed?
有没有办法用for"循环遍历每个条目并检查每个条目的长度.或者有没有命令可以检查任何框是否为空?
Is there a way to go through each of them with a 'for' loop and check the length of each entry. Or is there a command to check if any box is empty?
推荐答案
您可以使用 Tkinter.Entry.get
方法.
You can get the content of the entry using Tkinter.Entry.get
method.
使用len
函数和get
方法检查条目的长度:
Check the length of the entry using len
function and get
method:
if len(entry_object.get()) == 0: # empty!
# do something
或更优选:
if not entry_object.get(): # empty! (empty string is false value)
# do something
<小时>
使用 for
循环检查几个入口小部件:
Use for
loop to check several entry widgets:
for entry in entry_list:
if not entry_object.get():
# empty!
您应该事先填充entry_list
.
手动:
entry_list = []
entry = Entry(...)
...
entry_list.append(entry)
entry = Entry(...)
...
entry_list.append(entry)
或者,使用 winfo_children
方法(获取所有条目):
entry_list = [child for child in root_widget.winfo_children()
if isinstance(child, Entry)]
这篇关于如何检查 Tkinter 中是否有任何输入框为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!