简体   繁体   English

如何在Tkinter Python中冻结键选项卡的使用

[英]How to freeze use of key tab in tkinter python

I am trying to make a simple application that scrambles keyboard letters while typing. 我正在尝试制作一个简单的应用程序,该应用程序在键入时会打乱键盘字母。 I am using python along with tkinter. 我正在与tkinter一起使用python。 I have a text widget and i need to disable the key tab in my application. 我有一个文本小部件,我需要在应用程序中禁用键选项卡。 I tried it using following code. 我尝试使用以下代码。

text.bind("<Tab>", no_op)

Here no_op is the function given below: 这里no_op是下面给出的函数:

def no_op(self):
    return "break"        

But I am not getting the expected result. 但是我没有得到预期的结果。 I am posting the whole code below. 我将在下面发布整个代码。

import Tkinter as tk

def onKeyPress(event):
    first=event.char
    second=ord(first)
    if second==32:
        second=chr(second)
        text.insert('end', '%s' % (second ))
    elif second==8:
        length = len(text.get(1.0, 'end'))
        contents = text.get(1.0, 'end')
        newcon = contents[:-2]
        #text.insert('end', '%s' % (length ))
        text.delete(1.0,'end')
        text.insert('end', '%s' % (newcon ))
    elif(second>=65 and second<=90 or second>=97 and second<=122):
        second=chr(second+3)
        text.insert('end', '%s' % (second ))


def no_op(self):
    return "break"


root = tk.Tk()
root.config(cursor='none')
#root.attributes('-zoomed',True)
text = tk.Text(root, background='white', foreground='black', font=('Comic Sans MS', 12))
text.pack(expand=True,)

text.bind("<Tab>", no_op)
text.bind("<Button-1>", no_op)
text.config(cursor="none")
root.bind('<KeyPress>', onKeyPress)
root.mainloop()

( Note : The problem is that when tab is pressed when some other widget has focus, the text cursor comes in the text area. Then, if I press any letter say,'a' both 'a' and 'd' is inserted to text field. I want to fix that.) 注意 :问题是当其他小部件具有焦点时按下Tab键时,文本光标进入文本区域。然后,如果我按任何字母,则“ a”和“ d”都插入到文本字段。我要解决此问题。)

Your problem isn't with the tab key, your problem is focus management. 您的问题不在于Tab键,问题在于焦点管理。 You've made your code work only if the text widget never gets keyboard focus. 仅当文本窗口小部件从未获得键盘焦点时,您才能使代码工作。 There are at least two solutions: 至少有两种解决方案:

  • continue down the path of preventing the user from focusing on the text widget 继续阻止用户专注于文本小部件
  • allow focus on the text widget, and adjust your bindings accordingly 允许专注于文本小部件,并相应地调整绑定

For the first, instead of trying to change the behavior of the tab (and also the shift-tab), you can merely move the focus whenever the text widget gets it. 首先,您无需尝试更改选项卡(以及Shift-Tab)的行为,而只需在文本小部件获得焦点时就移动焦点。 For example: 例如:

text.bind("<FocusIn>", lambda event: root.focus_set())

That will prevent the text widget from ever getting focus, and your code should work. 这将阻止文本小部件获得焦点,并且您的代码应该可以工作。

Another solution is to modify your <KeyPress> binding to be on the text widget rather than the root widget, and then simply reject all handling of key presses. 另一个解决方案是将<KeyPress>绑定修改为在文本小部件上而不是在根小部件上,然后仅拒绝对按键的所有处理。 That means to do text.bind('<KeyPress>', ...) rather than root.bind... . 这意味着要做text.bind('<KeyPress>', ...)而不是root.bind... You then need to modify onKeyPress to return "break" to prevent the default text widget bindings from happening. 然后,您需要修改onKeyPress以返回"break"以防止发生默认的文本小部件绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM