Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting tab key to go to next field with Tkinter Text (instead of indent) [duplicate]

I have searched and spent about literally two hours trying to solve this myself, but my efforts have failed me. My goal is to type a message in the text box, press tab to go to the email button then press enter to send the email.

Right now, pressing tab in the TEXT field creates word-processor-style indents, instead of moving to the next field.

The relevant part of the code is this:

from tkinter import *

#Build Window

def focus():
    b.focus_set()

window = Tk()
window.title("What's Your Message?")
window.configure(background="black")
Label (window, text="Type Your Message:\n", bg="Black", fg="white", font="none 25 bold").pack(anchor=N)

e = Text(window, width=75, height=10)
e.pack()
e.focus_set()
e.bind("<Tab>", lambda e: focus())

b = Button(window, text="Send Email", takefocus=True, font="none 15 bold", width=10, command=lambda: click())
b.bind("<Return>", lambda e: click())
b.bind("<Tab>", lambda e: focus())
b.pack()

Can you tell me how to have the tab button no longer indent text in the text box, but instead move focus to the button? Right now, SHIFT + TAB works, but I'd like to understand how to get just hitting TAB to work.

Thank you for your time and help!

like image 589
PythonNeophyte Avatar asked Oct 26 '25 17:10

PythonNeophyte


1 Answers

Shift-tab is used for reverse-traversal.

I think the following page will be of use to you: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/focus.html

From this page, there is one paragraph that may assist you,

To sum up: to set up the focus traversal order of your widgets, create them in that order. Remove widgets from the traversal order by setting their takefocus options to 0, and for those whose default takefocus option is 0, set it to 1 if you want to add them to the order.

*Edit: Looks like a duplicate of Change the focus from one Text widget to another

**Edit2: So... as taken from the stackoverflow post above, the following will do exactly what you request:

def focus_next_widget(event):
    event.widget.tk_focusNext().focus()
    return("break")

window = Tk()
window.title("What's Your Message?")
window.configure(background="black")
Label (window, text="Type Your Message:\n", bg="Black", fg="white", font="none 25 bold").pack(anchor=N)

e = Text(window, width=75, height=10)
e.bind("<Tab>", focus_next_widget)
like image 172
Sir Donnie Avatar answered Oct 29 '25 07:10

Sir Donnie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!