python - 是否可以在 textview gtk 中获取光标坐标
问题描述
我正在使用 Gtk 使用 Pythons 开发一个应用程序gi.repository
。我想知道是否可以在文本视图中移动光标时获取光标相对于屏幕的坐标。
例如,它返回光标的 x1, x2, y1, y2。
解决方案
是的,有可能。只需绑定TextView
to "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()
推荐阅读
- c++ - 如何调用下面定义的函数?
- javascript - 根据我在 rails/react 应用程序上的 api 调用状态更新我的视图
- php - 定义数组的方法 - 用于 php 5 而不是 php 7
- c# - Xamarin.iOS 对象丢失属性成员
- c++ - 定义(重载)自定义 C++ 运算符
- vue.js - Vue.js 选择所有选项
- node.js - 将本地节点 C++ 插件导入 Vue.js
- c# - CairoSharp Nuget 包找不到 cairo.dll
- laravel - 如何在 Laravel 和 Vue.js 的自定义 js 文件中使用组件?
- excel - 如何从 Excel VBA 中获取 Windows 资源管理器的选定文件?执行此操作需要什么 de VBA 代码?