绑定事件在输入字符之前运行
我正在制作一个具有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Bryan 建议的解决方案之外,您还可以绑定
而不是
。然后回调将在添加换行符后执行: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:简短的答案是,您的绑定发生在内置密钥绑定之前。因此,在TKINTER实际插入NewLine之前,请调用您的函数。
您应该让代码插入newline,执行其他操作,然后返回“休息”以防止默认行为发生。
有关如何处理绑定的更全面说明,请参见
这是一个工作示例:
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: