光标位置在TextView中已更改 [英] Cursor position changed in textview

查看:75
本文介绍了光标位置在TextView中已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PyGTK中,无论何时将光标移到我的应用程序的textview中,我都希望获得光标的当前位置.因此,我需要创建一个回调函数并连接到信号.但是我不确定从哪里得到该信号.

In PyGTK, I want to get the current position of the cursor whenever the cursor is moved inside a textview in my app. So I need to create a callback function and connect to a signal. But I'm not sure from where to get that signal.

推荐答案

您要监视缓冲区的cursor-position属性,请查看下面的示例以监视光标位置.

You want to monitor the cursor-position property of the buffer, look at the example here below for monitoring the cursor position.

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)

这篇关于光标位置在TextView中已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆