禁用 tkinter 键盘快捷键 (2) [英] disable tkinter keyboard shortcut (2)

查看:38
本文介绍了禁用 tkinter 键盘快捷键 (2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提议继续讨论 禁用 tkinter 键盘快捷键:我有Tkinter 也使用的事件的事件处理程序,以便我的 prog &Tkinter 交互不佳.

I'm proposing a continuation of the discussion in disable tkinter keyboard shortcut: I have an event handler for an event that Tkinter also uses, so that my prog & Tkinter interact badly.

由于这是一个我一直无法解决的问题,我在这里重新提出,我尝试将其归结为以下代码中的最简单形式:

Since it is a problem that I've been unable to solve I'm re-proposing here, where I tried to boil it down to the simplest form in the following code:

#!/usr/bin/env python

from Tkinter import *
import tkFont

def init():
    global root,text

    root = Tk()
    root.geometry("500x500+0+0")
    dFont=tkFont.Font(family="Arial", size=10)

    text=Text(root, width=16, height=5, font=dFont)
    text.pack(side=LEFT, fill=BOTH, expand = YES)

    root.bind("<Control-b>", setbold)

    text.tag_config("b",font=('Verdana', '10', 'bold' ))
    text.tag_config("i",font=('Verdana', '10', 'italic' ))

def removeformat(event=None):
    text.tag_remove('b',SEL_FIRST,SEL_LAST)
    text.tag_remove('i',SEL_FIRST,SEL_LAST)

def setbold(event=None):
    removeformat()
    text.tag_add('b', SEL_FIRST,SEL_LAST)
    text.edit_modified(True)

def main():
    init()        
    mainloop()


if __name__ == '__main__':
    main()

它应该做的只是生成一个文本窗口,您可以在其中写入.选择一些文本并按 Ctrl+B 程序应该删除任何预先存在的标签,然后为其分配将文本设置为粗体的b"标签.

What it should do is simply to produce a text window where you write into. Selecting some text and pressing Ctrl+B the program should remove any preexisting tag, then assign to it the 'b' tag that sets the text to bold.

相反,第一个 tag_remove 发生了异常,告诉我 text 不包含任何标记为sel" 的字符.

What instead happens is an exception at the first tag_remove, telling me that text doesn't contain any characters tagged with "sel".

建议使用 return 'break' 是没有用的,因为选择在 setbold() 有机会行动之前就消失了......

The suggestion to use a return 'break' is of no use, since the selection disappears before setbold() has any chance to act...

推荐答案

在文本小部件上设置绑定,而不是在根上.(整个顶级绑定小部件类绑定之后处理——标准绑定所在——并且那些之后处理> 小部件实例绑定,这就是你想在这里使用的.)你需要这样做 'break';它抑制了随后的绑定.(如果之后您遇到任何问题,可能是默认情况下焦点错误,但这很容易解决.)

Set your binding on the text widget, not on the root. (Whole toplevel bindings are processed after widget class bindings – where the standard <Control-Key-b> binding is – and those are processed after the widget instance bindings, which is what you want to use here.) And you need to do that 'break'; it inhibits the subsequent bindings. (If you're having any problems after that, it's probably that the focus is wrong by default, but that's easy to fix.)

唯一的另一种选择是重新配置绑定标签,以便在顶级绑定之后处理类绑定,但这样做的后果非常微妙且影响深远;你应该使用我第一段中更简单的方法,因为这是处理这些事情的正常方法.

The only other alternative is to reconfigure the bindtags so that class bindings are processed after toplevel bindings, but the consequences of doing that are very subtle and far-reaching; you should use the simpler approach from my first paragraph instead as that's the normal way of handling these things.

这篇关于禁用 tkinter 键盘快捷键 (2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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