Tkinter:当按“Enter"时,它会转到下一个文本框 [英] Tkinter: When press 'Enter', then it goes to the next text box
问题描述
所以我的老师告诉我学习 Tkinter 做我自己,我试着去理解它.这是我正在查看的程序.
So my teacher told me to learn Tkinter be myself and I try to understand it. Here's is the program I'm looking at.
它要求输入姓名和姓氏.(是的,西班牙语是我的母语)
It asks for a name and last name. (Yeah, Spanish is my native language)
当我输入完姓名后,我只想按回车键转到下一个是姓氏的文本框.
When I finish inserting the name, I just want to press enter to go to the next text box which is last name.
from tkinter import *
master = Tk()
Label(master, text="Ingrese sus nombres: ").grid(row=0)
Label(master, text="Ingrese sus apellidos: ").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()
推荐答案
一个快速而肮脏的方法是将第一个 Entry
小部件bind
关注其他小部件:
A quick and dirty way would be to bind
the first Entry
widget to a function that switches the focus to the other widget:
def go_to_next_entry(event):
e2.focus_set() # focus_set() switches the focus to the new widget
e1.bind('<Return>', go_to_next_entry)
# or, if you are familiar with lambdas, simply:
# e1.bind('<Return>', lambda e: e2.focus_set())
.bind
方法需要作为第一个参数,代表用户交互类型的字符串,作为第二个参数,一个函数的单个参数(事件参数.在这种情况下,您不必关心它,但是如果您正在监视光标的移动,则该事件可以为您提供其坐标.)
The .bind
method expects as a first argument a string representing the type of user interaction, and as a second argument, a function of a single argument (the event argument. In this case you do not have to care about that, but if you were monitoring the movement of the cursor, the event could give you its coordinates.)
对于更一般的方式,事情变得更加棘手,这种方法似乎比任何东西都更像是一种黑客.无论如何,如果您有很多条目,您可以像这样自动执行该方法:
For a more general way, things get trickier and this method seems more an hack than anything. Anyway, if you have many entries, you can automate the method like so:
查找所有
Entry
小部件.它们是master
的子级,您可以从master.winfo_children()
获取它们,它按照它们在代码中声明的顺序给出子级.但是要小心,因为你得到了所有子元素(标签
,也是),所以你必须过滤子元素(在这种情况下过滤器是按类型"它是通过isinstance
) 完成的:
Find all
Entry
widgets. They are children ofmaster
and you get them frommaster.winfo_children()
, which gives the children in the order they were declared in the code. Be careful, though, because you get all the children (theLabels
, too), so you have to filter the children (in this case the filter is "by type" and it is done withisinstance
):
entries = [child for child in master.winfo_children()
if isinstance(child, Entry)]
然后,您定义将焦点切换到所需小部件的函数:
Then, you define the function that switches the focus to the desired widget:
def go_to_next_entry(event, entry_list, this_index):
next_index = (this_index + 1) % len(entry_list)
entry_list[next_index].focus_set()
部分 next_index = (this_index + 1) % len(entries)
循环遍历条目(如果在最后一个条目处按 Return,则会转到第一个一).
The part next_index = (this_index + 1) % len(entries)
cycles over the entries (if you press Return at the last entry, you go to the first one).
最后,您将 switch 函数绑定到每个条目:
Finally, you bind the switch function to each entry:
for idx, entry in enumerate(entries):
entry.bind('<Return>', lambda e, idx=idx: go_to_next_entry(e, entries, idx))
可怕的部分是:lambda e, idx=idx: go_to_next_entry(e, entries, idx)
.这里的重要部分是 lambda
用于创建另一个函数(很像 def
),它有 2 个参数,而不是go_to_next_entry
.idx=idx
部分使得仅使用 1 个参数即可调用新创建的函数成为可能(根据 .bind
的要求.)了解为什么 idx=idx
实际上很重要,不能省略,看看动态生成 Tkinter 按钮(和按钮有关,但原理是一样的.)
The scary part is: lambda e, idx=idx: go_to_next_entry(e, entries, idx)
. The important part here, is that lambda
is used to create another function (much like def
) that has 2 arguments instead of the 3 required by go_to_next_entry
. The idx=idx
part makes it possible to call the newly-created function with just 1 parameter (as required by .bind
.) To see why idx=idx
is actually important and could not be omitted, have a look at Generate Tkinter buttons dynamically (which is about buttons, but the principle is the same.)
完整代码:
from tkinter import *
master = Tk()
Label(master, text="Ingrese sus nombres: ").grid(row=0)
Label(master, text="Ingrese sus apellidos: ").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
def go_to_next_entry(event, entry_list, this_index):
next_index = (this_index + 1) % len(entry_list)
entry_list[next_index].focus_set()
entries = [child for child in master.winfo_children() if isinstance(child, Entry)]
for idx, entry in enumerate(entries):
entry.bind('<Return>', lambda e, idx=idx: go_to_next_entry(e, entries, idx))
mainloop()
这篇关于Tkinter:当按“Enter"时,它会转到下一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!