【发布时间】:2015-07-24 18:01:27
【问题描述】:
我正在尝试制作一个简单的应用程序,可以在打字时打乱键盘字母。我正在使用 python 和 tkinter。我有一个文本小部件,我需要在我的应用程序中禁用键选项卡。我用下面的代码试了一下。
text.bind("<Tab>", no_op)
这里 no_op 是下面给出的函数:
def no_op(self):
return "break"
但我没有得到预期的结果。我在下面发布整个代码。
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()
(注意:问题是当其他一些小部件具有焦点时按下选项卡时,文本光标进入文本区域。然后,如果我按任何字母说,'a' both 'a' 和 'd' 被插入到文本字段中。我想解决这个问题。)
【问题讨论】:
-
我没有得到预期的结果:请更具体。在我的电脑上,你的代码可以工作(一旦我修复了缩进)。
-
您的代码在 OSX 上对我来说可以正常工作——按 Tab 没有任何作用。
-
问题是当按下tab时,文本光标进入文本区域。然后如果我按下任何字母说,'a''a'和'd'都插入到文本字段中.我想解决这个问题。