问题
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!
回答1:
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)
来源:https://stackoverflow.com/questions/52061933/getting-tab-key-to-go-to-next-field-with-tkinter-text-instead-of-indent