2

我正在制作一个基本的 IDE,行号将类似于 IDLE,但我不想将每个可能的键绑定到一个事件,该事件会更改包含当前行/列的框。是否有某种“on change”或“one cursor move”事件内置到 Tkinter 中,或者更具体地说,ScrolledText。如果没有,那么如果有人能指出我正确的方向,那就太好了。

谢谢!

4

1 回答 1

5

There's nothing built-in per se, but what you want to do is possible if you're willing to be creative.

At it's core a text widget is a tcl command, and this command is called whenever something happens to the text widget: text is inserted, deleted, the cursor changes, etc. The nature of tcl is that we can replace this command with our own command. And since we can do that, we can detect certain changes, and call our own function before or after.

It sounds complicated, and it is. On a positive note, it's foolproof once you have it working, and it means you don't have to do any custom bindings. To see a complete working example, see this answer to the question binding to cursor movement doesnt change INSERT mark.

The scrolled text widget is just a thin wrapper around a regular text widget, so this answer will work with just a tiny bit of tweaking (you'll need the reference to the text widget used by the scrolledtext widget). The wrapper is so thin, however, that I recommend not using it since adding scrollbars to a text widget is trivial.

于 2013-09-09T11:07:00.673 回答