绑定事件在输入字符之前运行

发布于 2025-01-19 17:31:37 字数 1278 浏览 1 评论 0原文

我正在制作一个具有TKINTER的特殊文本小部件,该小部件应该具有工作凹痕。为了使缩进工作要工作,我借助将选项卡放到Enter按钮的功能。此绑定在实际键入字符之前进行的绑定效果很好,但它不会与此功能相关。有人可以帮我吗?

工作绑定:

def double_parentheses(self, event):
        main_text_box_anchor = str(self.text.index("insert")).split(".")
        self.text.insert(INSERT, ")")
        self.text.mark_set(INSERT, str(main_text_box_anchor[0]) + "." + 
        str(int(main_text_box_anchor[1])))
#later in the code
scroll.text.bind("(", scroll.double_parentheses)

这将括号按顺序排列,其中中间的插入

无法正常工作:

def new_line_indent(self, event):
        line = self.text.get("insert linestart", "insert lineend")
        editable_line = str(line)
        if editable_line != "":
            if editable_line[-1] == ":":
                if "\t" in editable_line:
                    for i in range(editable_line.count("\t")):
                        self.text.insert("insert", "\t")
                else:    
                    self.text.insert("insert", "\t")
            elif "\t" in editable_line:
                for i in range(editable_line.count("\t")):
                    self.text.insert("insert", "\t")
#Later in the code
scroll.text.bind("<Return>", scroll.new_line_indent)

这将标签放入其中,但在创建新行之前会做到这一点,我不知道为什么。我在做什么错?

I am making a special Text widget with tkinter that is supposed to have working indentation. To get the indentation to work I binded the function that puts down the tabs to the Enter button. This binding worked fine before with the bind going after the actual typed character but it won't with this function. Can someone help me out?

Working bind:

def double_parentheses(self, event):
        main_text_box_anchor = str(self.text.index("insert")).split(".")
        self.text.insert(INSERT, ")")
        self.text.mark_set(INSERT, str(main_text_box_anchor[0]) + "." + 
        str(int(main_text_box_anchor[1])))
#later in the code
scroll.text.bind("(", scroll.double_parentheses)

This puts the parentheses in order with the insert in the middle of them

Not working:

def new_line_indent(self, event):
        line = self.text.get("insert linestart", "insert lineend")
        editable_line = str(line)
        if editable_line != "":
            if editable_line[-1] == ":":
                if "\t" in editable_line:
                    for i in range(editable_line.count("\t")):
                        self.text.insert("insert", "\t")
                else:    
                    self.text.insert("insert", "\t")
            elif "\t" in editable_line:
                for i in range(editable_line.count("\t")):
                    self.text.insert("insert", "\t")
#Later in the code
scroll.text.bind("<Return>", scroll.new_line_indent)

This puts the tabs in but it does it BEFORE the new line is created and I can't figure out why. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2025-01-26 17:31:37

除了 Bryan 建议的解决方案之外,您还可以绑定 而不是 。然后回调将在添加换行符后执行:

    def new_line_indent(self, event):
        # get previous line
        line = self.text.get("insert-1c linestart", "insert-1c lineend")
        editable_line = str(line)
        if editable_line != "":
            for i in range(editable_line.count("\t")):
                self.text.insert("insert", "\t")
            if editable_line[-1] == ":":
                self.text.insert("insert", "\t")

...
#Later in the code
scroll.text.bind("<KeyRelease-Return>", scroll.new_line_indent)

Other than the solution suggested by Bryan, you can bind <KeyRelease-Return> instead of <Return>. Then the callback will be executed after the newline is added:

    def new_line_indent(self, event):
        # get previous line
        line = self.text.get("insert-1c linestart", "insert-1c lineend")
        editable_line = str(line)
        if editable_line != "":
            for i in range(editable_line.count("\t")):
                self.text.insert("insert", "\t")
            if editable_line[-1] == ":":
                self.text.insert("insert", "\t")

...
#Later in the code
scroll.text.bind("<KeyRelease-Return>", scroll.new_line_indent)
迷爱 2025-01-26 17:31:37

这将选项卡放入其中,但是在创建新行之前就可以做到这一点,我不知道为什么。

简短的答案是,您的绑定发生在内置密钥绑定之前。因此,在TKINTER实际插入NewLine之前,请调用您的函数。

您应该让代码插入newline,执行其他操作,然后返回“休息”以防止默认行为发生。

有关如何处理绑定的更全面说明,请参见

这是一个工作示例:

def new_line_indent(self, event):
    # get the text of the current line
    line = self.text.get("insert linestart", "insert lineend")

    # insert the newline
    self.text.insert("insert", "\n")

    # insert indentation if line ends with ":"
    if line and line[-1] == ":":

        # get current indentation, then insert it
        leading_whitespace = self.get_leading_whitespace(line)
        self.text.insert("insert", leading_whitespace)

        # add an additional level of indentation
        self.text.insert("insert", "\t")

    # since we already inserted the newline, make sure that
    # the default bindings do not
    return "break"

def get_leading_whitespace(self, line):
    n = len(line) - len(line.lstrip())
    return line[:n]

This puts the tabs in but it does it BEFORE the new line is created and I can't figure out why.

The short answer is that your binding happens before the built-in key bindings. Thus, your function is called before the newline is actually inserted by tkinter.

You should have your code insert the newline, perform your other actions, then return "break" to prevent the default behavior from happening.

For a more thorough explanation of how bindings are processed, see this answer

Here's a working example:

def new_line_indent(self, event):
    # get the text of the current line
    line = self.text.get("insert linestart", "insert lineend")

    # insert the newline
    self.text.insert("insert", "\n")

    # insert indentation if line ends with ":"
    if line and line[-1] == ":":

        # get current indentation, then insert it
        leading_whitespace = self.get_leading_whitespace(line)
        self.text.insert("insert", leading_whitespace)

        # add an additional level of indentation
        self.text.insert("insert", "\t")

    # since we already inserted the newline, make sure that
    # the default bindings do not
    return "break"

def get_leading_whitespace(self, line):
    n = len(line) - len(line.lstrip())
    return line[:n]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文