Search code examples
pythonpython-3.xtkinterdevelopment-environmenttkinter-entry

Is there a function to switch "Tab" and "Enter" button in tkinter?


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.


Solution

  • 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()