-1

I'm creating a Gui with Tkinter with a lot of Entry widgets. However, I don't like the fact of clicking on Tab button to go from one entry to another. I would like it to be the Enter key that does this. Is there a function to make it happen ?? here is the list of all entries:

```python
entries = [self.entMath1, self.entMath2, self.entFran1, 
           self.entFran2, self.entSvt1, self.entSvt2, self.entEps1, 
           self.entEps2, self.entHg1, self.entHg2, self.entPc1, 
           self.entPc2, self.entAng1, self.entAng2, self.entPhi1, 
           self.entPhi2, self.entM1, self.entM2, self.entM3, 
           self.entM4]
for i in range(len(entries_1) - 1):
    entries_1[i].bind("<Return>", lambda e: entries_1[i].focus_set())
```

all these entries are placed with place() method.

2 Answers2

1

Have a read of this answer binding enter to a widget for the difference between <Return> and <Enter>

From that, you can build something like

import tkinter as tk
top = tk.Tk()
frm = tk.Frame(top)
frm.pack()

# List of entries
seq = [None]
next = []
entmax = 5
for ii in range(entmax):
    ent = tk.Entry(frm)
    ent.pack(side=tk.LEFT, padx=5, pady=5)
    seq.append(ent)
    next.append(ent)

# Now set the entries to move to the next field
for ix in range(1, entmax, 1):
    seq[ix].bind('<Return>', lambda e: next[ix].focus_set())

top.mainloop()
cup
  • 7,589
  • 4
  • 19
  • 42
1

You can bind the <Return> key event on the Entry widget. Then in the bind callback, get the next widget in the focus order by .tk_focusNext() and move focus to this widget:

import tkinter as tk

root = tk.Tk()

for i in range(10):
    e = tk.Entry(root)
    e.grid(row=0, column=i)
    e.bind('<Return>', lambda e: e.widget.tk_focusNext().focus_set())

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34