使用 Tkinter 文本获取 Tab 键以转到下一个字段(而不是缩进) [英] Getting tab key to go to next field with Tkinter Text (instead of indent)

查看:37
本文介绍了使用 Tkinter 文本获取 Tab 键以转到下一个字段(而不是缩进)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

你能告诉我如何让标签按钮不再在文本框中缩进文本,而是将焦点移到按钮上吗?现在,SHIFT + TAB 有效,但我想了解如何仅按 TAB 即可工作.

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.

感谢您的时间和帮助!

推荐答案

Shift-tab 用于反向遍历.

Shift-tab is used for reverse-traversal.

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

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,

总而言之:要设置小部件的焦点遍历顺序,请按该顺序创建它们.通过将它们的 takefocus 选项设置为 0 来从遍历顺序中删除小部件,对于那些默认的 takefocus 选项为 0 的部件,如果要将它们添加到顺序中,请将其设置为 1.

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.

*看起来像 将焦点从一个文本小部件更改为另一个的副本

**所以......从上面的stackoverflow帖子中获取,以下内容将完全符合您的要求:

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

这篇关于使用 Tkinter 文本获取 Tab 键以转到下一个字段(而不是缩进)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆