您如何插入文本小部件,而不会触发<< gt;>>绑定?

发布于 2025-01-23 05:49:20 字数 631 浏览 1 评论 0原文

我有一个文本小部件,我想自动将文本插入打开窗口时,然后我也想检测用户使用“<<< modified>>>>>>>”的每次修改文本时。结合。这并不重要,但是我记得早些时候执行了相同的代码,并且并没有触发“修改”。具有约束力,但现在已经脱颖而出,所以我更加困惑。这是代码示例:

from tkinter import *

root = Tk()
textBox = Text(root, font=("Helvetica", 20))
textBox.pack()

textBox.insert(END, "test")

def on_modifyTextBox():
    print("<<Modified>> bind triggered")

textBox.bind("<<Modified>>", lambda e: on_modifyTextBox())

root.mainloop()

输出:

<<Modified>> bind triggered

因此,如何默认情况下将一些文本插入文本窗口小部件,然后将其绑定到&lt;&lt;修改没有它本身触发,但仅由用户触发?

I have a Text widget which I want to automatically insert text to when the window is opened, and then I also want to detect every time that the user modifies the text using the "<< Modified>>" binding. This is not important, but I remember doing the same code earlier and it didn't trigger the "<< Modified>>" binding, but now out of the blue it does, so I'm even more confused. This is the code sample:

from tkinter import *

root = Tk()
textBox = Text(root, font=("Helvetica", 20))
textBox.pack()

textBox.insert(END, "test")

def on_modifyTextBox():
    print("<<Modified>> bind triggered")

textBox.bind("<<Modified>>", lambda e: on_modifyTextBox())

root.mainloop()

Output:

<<Modified>> bind triggered

So how do I insert some text to the Text widget by default but then later bind it to << Modified>> without it triggering by itself, but only triggered by the user?

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

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

发布评论

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

评论(2

标点 2025-01-30 05:49:20

绑定到&lt;键&gt;&lt; keyRelease&gt;在技术上确实有效,而我可以绑定到它,唯一的问题是即使键也是如此不要修改文本小部件被按下(例如ctrl或Backspace)是触发的。

Binding to <KeyPress> or <KeyRelease> does work technically, and I could just bind to that instead, the only problem with that would be that even when keys that don't modify the text widget are pressed (like Ctrl or Backspace) it is triggered.

暗恋未遂 2025-01-30 05:49:20

我也发现了同样的问题。看来这是由于首次插入文本后,文本小部件的修改标志被设置为True。将“测试”测试插入小部件后,请使用以下代码行:

textBox.edit_modified(False)

现在应该允许用户触发事件。

I found this same issue also. It looks like this is due to the modified flag of the text widget being set to true once you first insert the text. After you insert the 'test' test into the widget, use the following line of code:

textBox.edit_modified(False)

This should now allow the event to be triggered by the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文