如何绑定tkinter中的退格键删除多个字符? [英] How to bind the backspace key in tkinter to delete more than one character?

查看:55
本文介绍了如何绑定tkinter中的退格键删除多个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建绑定,让我按 Tab 键插入预定义数量的空格,然后按退格键删除那么多空格,基于一个变量.

I am wanting to create bindings that let me press tab to insert a predefined number of spaces, and then press backspace to delete that many spaces, based on a variable.

如何在用户按下退格键时删除预定数量的空格?我不知道如何删除多个字符,当我试图解决这个问题时,绑定删除了错误数量的字符.

How can I delete a pre-determined number of spaces when the user presses the backspace key? I don't know how to delete multiple characters, and when I've attempted to solve this, the binding deletes the wrong number of characters.

推荐答案

这个问题有两个部分.第一部分是关于如何一次删除多个字符.第二部分是如何在绑定到退格键的绑定中使用它

There are two parts to this question. The first part is related to how to delete more than one character at a time. The second part is how to use that in a binding tied to the backspace keys

文本小部件的delete方法有两个索引,将删除这些索引之间的字符.Tkinter 文本索引可以是相对的通过对索引应用修饰符.例如,要引用四个插入点之前的字符,您可以使用索引 "insert" 加上修饰符 "-4 chars".

The delete method of the text widget takes two indexes, and will delete the characters between those indexes. Tkinter text indexes can be relative by applying modifiers to an index. For example, to reference the four characters before the insertion point you can use the index "insert" plus the modifier "-4 chars".

示例:

self.text.delete("insert -4 chars", "insert")

因为这些索引都是普通的字符串,所以如果要使用变量,可以使用字符串格式化.

Because these indexes are normal strings, you can use string formatting if you want to use a variable.

示例:

tabWidth = 4
self.text.delete("insert -%d chars" % tabWidth, "insert")

使用具有绑定的函数

要在用户按下退格键时运行函数,您可以绑定 事件的函数.这个函数会被传递代表事件的一个参数.

Using the function with a binding

To run a function when the user presses the backspace key, you can bind a function to the <BackSpace> event. This function will be passed one argument which represents the event.

例如:

self.text.bind("<BackSpace>", self.do_backspace)
...
def do_backspace(self, event):
    ...

对标准键进行自定义绑定的一个重要部分是要知道默认情况下您的绑定不会替换默认行为.例如,如果您的绑定删除一个字符然后返回,则两个字符将被删除,因为您的绑定将删除一个,而默认绑定将删除一个.

An important part of doing custom bindings to standard keys is to know that your binding by default will not replace the default behavior. For example, if your binding deletes one character and then returns, two characters will get deleted because your binding will remove one and the default binding will remove one.

覆盖此行为的方法是返回字符串 "break".因为您的自定义绑定发生在默认绑定之前,所以默认绑定会看到您返回了break"并且什么也不做.这使得覆盖默认行为或在保持默认行为的同时执行其他工作变得非常简单.

The way to override this behavior is to return the string "break". Because your custom binding happens before the default binding, the default binding will see that you returned "break" and do nothing. This makes it very simple to either override the default behavior or do additional work while keeping the default behavior.

综合起来,这就是你如何定义一个函数,如果前四个字符是四个连续空格,则删除它们,如果不是,则执行默认行为:

Putting it all together, this is how you can define a function that deletes the previous four characters if they are four consecutive spaces, and do the default behavior if not:

def do_backspace(self, event):
    # get previous <tabWidth> characters; if they are all spaces, remove them

    previous = self.text.get("insert -%d chars" % self.tabWidth, "insert")
    if previous == " " * self.tabWidth:
        self.text.delete("insert-%d chars" % self.tabWidth, "insert")

        # return "break" so that the default behavior doesn't happen
        return "break"

    # if we get to here, we'll just return. That allows the default
    # behavior to run

把它们放在一起

这是一个完整的工作示例,当您按 T​​ab 键,然后按退格键删除四个空格:

Putting it all together

Here is a complete working example that will insert four spaces when you press tab, and remove four spaces when you press backspace:

import tkinter as tk

def do_tab(event):
    text.insert("insert", " " * tabWidth)
    # return "break" so that the default behavior doesn't happen
    return "break"

def do_backspace(event):
    # get previous <tabWidth> characters; if they are all spaces, remove them
    previous = text.get("insert -%d chars" % tabWidth, "insert")
    if previous == " " * tabWidth:
        text.delete("insert-%d chars" % tabWidth, "insert")
        # return "break" so that the default behavior doesn't happen
        return "break"

    # if we get to here, we'll just return. That allows the default
    # behavior to run

root = tk.Tk()
tabWidth = 4

text = tk.Text(root)
text.pack(fill="both", expand=True)

text.bind("<Tab>", do_tab)
text.bind("<BackSpace>", do_backspace)

root.mainloop()

这篇关于如何绑定tkinter中的退格键删除多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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