是否有可能在textview gtk中获取光标坐标
Is it possible to get cursor coordinates in textview gtk
我正在使用 Python 开发 Gtk 应用 gi.repository
。我想知道是否有可能在文本视图中移动光标时获取相对于屏幕的坐标。
例如它 returns 光标的 x1, x2, y1, y2.
是的,这是可能的。只需将 TextView
绑定到 "event"
,并在处理程序函数中检查事件类型是否为 Gdk.EventType.MOTION_NOTIFY
。
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk
win = Gtk.Window()
def on_event(widget, event):
# Check if the event is a mouse movement
if event.type == Gdk.EventType.MOTION_NOTIFY:
print(event.x, event.y) # Print the mouse's position in the window
t = Gtk.TextView()
t.connect("event", on_event)
win.add(t)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
我正在使用 Python 开发 Gtk 应用 gi.repository
。我想知道是否有可能在文本视图中移动光标时获取相对于屏幕的坐标。
例如它 returns 光标的 x1, x2, y1, y2.
是的,这是可能的。只需将 TextView
绑定到 "event"
,并在处理程序函数中检查事件类型是否为 Gdk.EventType.MOTION_NOTIFY
。
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk
win = Gtk.Window()
def on_event(widget, event):
# Check if the event is a mouse movement
if event.type == Gdk.EventType.MOTION_NOTIFY:
print(event.x, event.y) # Print the mouse's position in the window
t = Gtk.TextView()
t.connect("event", on_event)
win.add(t)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()