Why does the Text widget event <<Modified>> get triggered when specifically using <Control-o>?

huangapple go评论73阅读模式
英文:

Why does the Text widget event <<Modified>> get triggered when specifically using <Control-o>?

问题

I've come across a bug that I can't seem to understand. I have a tkinter Text widget that has a bind that triggers on text modification. For some reason this event gets triggered when I use the key combination <Control-o> even though it shouldn't, as it doesn't modify the contents of the Text widget.

Here comes the weird part: this only occurs with <Control-o>. I have made a simple program to demonstrate the problem. Other than special preassigned key combinations such as <Control-i> that actually modify the content, no other combination behaves like this.

Why does this occur for <Control-o> specifically? And how do I prevent it?

英文:

I've come across a bug that I can't seem to understand. I have a tkinter Text widget that has a bind that triggers on text modification. For some reason this event gets triggered when I use the key combination <Control-o> even though it shouldn't, as it doesn't modify the contents of the Text widget.

Here comes the weird part: this only (occurs with <Control-o>. I have made a simple program to demonstrate the problem. Other than special preassigned key combinations such as <Control-i> that actually modify the content, no other combination behaves like this.

Why does this occur for <Control-o> specifically? And how do I prevent it?

import tkinter as tk
root = tk.Tk()
txt = tk.Text(root)
txt.pack()
root.bind(&quot;&lt;Control-u&gt;&quot;, lambda e: print(&quot;doesn&#39;t trigger&quot;))
root.bind(&quot;&lt;Control-o&gt;&quot;, lambda e: print(&quot;somehow triggers&quot;))
txt.bind(&quot;&lt;&lt;Modified&gt;&gt;&quot;, lambda e: print(&quot;text got modified!&quot;)) # (keep in mind that this will only get triggered once)

答案1

得分: 7

The default binding on the text widget for <Control-o> adds a newline. This is from the section bindings in the official Tcl/Tk documentation for the text widget:

Control-o opens a new line by inserting a newline character in front of the insertion cursor without moving the insertion cursor.

Returning the string "break" from any binding prevents any further processing of the event. So, you can add a binding on the text widget for control-o that returns the string "break". Since your binding is handled before the default bindings for the widget, this will effectively prevent the default binding from modifying the widget.

txt.bind("<Control-o>", lambda e: "break")
英文:

The default binding on the text widget for &lt;Control-o&gt; adds a newline. This is from the section bindings in the official Tcl/Tk documentation for the text widget:

> Control-o opens a new line by inserting a newline character in front of the insertion cursor without moving the insertion cursor.

Returning the string "break" from any binding prevents any further processing of the event. So, you can add a binding on the text widget for control-o that returns the string "break". Since your binding is handled before the default bindings for the widget, this will effectively prevent the default binding from modifying the widget.

txt.bind(&quot;&lt;Control-o&gt;&quot;, lambda e: &quot;break&quot;)

huangapple
  • 本文由 发表于 2023年6月14日 23:51:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定