python - 使用 Tkinter Text 使用 Tab 键转到下一个字段(而不是缩进)

标签 python text tkinter tabs

我已经搜索并花了大约两个小时试图自己解决这个问题,但我的努力失败了。我的目标是在文本框中键入消息,按 Tab 转到电子邮件按钮,然后按 Enter 发送电子邮件。

现在,在 TEXT 字段中按 Tab 键会创建文字处理器样式的缩进,而不是移至下一个字段。

代码的相关部分是这样的:

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

你能告诉我如何让选项卡按钮不再缩进文本框中的文本,而是将焦点移到按钮上吗?现在,SHIFT + TAB 可以工作,但我想了解如何只需按 TAB 即可工作。

感谢您的时间和帮助!

最佳答案

Shift-tab 用于反向遍历。

我认为以下页面对您有用: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/focus.html

此页中的一段内容可能会对您有所帮助,

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.

*编辑: 看起来像 Change the focus from one Text widget to another 的重复项

**编辑2: 所以...正如上面的 stackoverflow 帖子所示,以下内容将完全按照您的要求进行:

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)

关于python - 使用 Tkinter Text 使用 Tab 键转到下一个字段(而不是缩进),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52061933/

相关文章:

python - 在 Google App Engine (GAE) 上运行 Boto

python - 使用 reST 嵌套 Sphinx 扩展指令

Java:如何使用变量右对齐文本

python - 让按钮改变列表框的内容

python - Tkinter 奇怪的框架大小

python - 我如何在弹出窗口中刷新答案

python - 为什么 Google 搜索会返回 HTTP 错误 403?

python - Selenium - 引发 httplib.BadStatusLine

delphi - Firemonkey Delphi TWebBrowser 获取文本/HTML

html - 使用 CSS 强制元素中的单行文本填充宽度