【发布时间】:2013-09-28 06:10:57
【问题描述】:
在 PyGTK 中,每当光标在我的应用程序的文本视图中移动时,我都想获取光标的当前位置。所以我需要创建一个回调函数并连接到一个信号。 但我不确定从哪里得到这个信号。
【问题讨论】:
-
请回答 Pygtk
标签: python textview cursor signals pygtk
在 PyGTK 中,每当光标在我的应用程序的文本视图中移动时,我都想获取光标的当前位置。所以我需要创建一个回调函数并连接到一个信号。 但我不确定从哪里得到这个信号。
【问题讨论】:
标签: python textview cursor signals pygtk
你想监控缓冲区的 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)
【讨论】:
notify::cursor-position 的原理是一样的。
notify::cursor-position?
notify::cursor-position