文本视图中的光标位置已更改

时间:2013-09-28 06:10:57

标签: python textview cursor signals pygtk

在PyGTK中,每当光标移动到我的应用程序的textview中时,我想获取光标的当前位置。所以我需要创建一个回调函数并连接到一个信号。 但我不知道从哪里得到那个信号。

1 个答案:

答案 0 :(得分:1)

您想要监视缓冲区的光标位置属性,请查看下面的示例,以监视光标位置。

from gi.repository import Gtk

class CursorSample(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self, application_id="org.app.CursorSample")

        self.buffer = Gtk.TextBuffer()
        self.buffer.connect("notify::cursor-position",
                            self.on_cursor_position_changed)

        self.tw = Gtk.TextView()
        self.tw.set_buffer(self.buffer)
        self.tw.props.wrap_mode = Gtk.WrapMode.CHAR

    def do_activate(self):
        main_window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        main_window.add(self.tw)
        self.add_window(main_window)
        main_window.set_position(Gtk.WindowPosition.CENTER)
        main_window.show_all()

    def on_cursor_position_changed(self, buffer, data=None):
        print buffer.props.cursor_position

if __name__ == "__main__":
    cursorsample = CursorSample()
    cursorsample.run(None)
相关问题