在 Python GTK3 中监听 Control + 鼠标滚轮滚动事件
Listen to Control + mouse wheel scroll event in Python GTK3
我正在构建一个 Python GTK 应用程序,我需要监听 "Ctrl+Mouse wheel" 事件。我想在 Webview 中实现 "zoom" 功能。我需要设置加速器吗?如果有,鼠标滚轮的键码是多少?
关于这些主题的文档并不多。
有什么帮助吗?
谢谢。
因为它发生了很多次,在 SO 中发帖后我找到了解决方案:)
这里是:
在 webview 上收听 "scroll event":
self.connect('scroll-event', self.on_scroll)
信号处理器
def on_scroll(self, widget, event):
""" handles on scroll event"""
# Handles zoom in / zoom out on Ctrl+mouse wheel
accel_mask = Gtk.accelerator_get_default_mod_mask()
if event.state & accel_mask == Gdk.ModifierType.CONTROL_MASK:
direction = event.get_scroll_deltas()[2]
if direction > 0: # scrolling down -> zoom out
self.set_zoom_level(self.get_zoom_level() - 0.1)
else:
self.set_zoom_level(self.get_zoom_level() + 0.1)
参考:GDK signal, keypress, and key masks
我正在构建一个 Python GTK 应用程序,我需要监听 "Ctrl+Mouse wheel" 事件。我想在 Webview 中实现 "zoom" 功能。我需要设置加速器吗?如果有,鼠标滚轮的键码是多少?
关于这些主题的文档并不多。
有什么帮助吗?
谢谢。
因为它发生了很多次,在 SO 中发帖后我找到了解决方案:)
这里是:
在 webview 上收听 "scroll event":
self.connect('scroll-event', self.on_scroll)
信号处理器
def on_scroll(self, widget, event):
""" handles on scroll event"""
# Handles zoom in / zoom out on Ctrl+mouse wheel
accel_mask = Gtk.accelerator_get_default_mod_mask()
if event.state & accel_mask == Gdk.ModifierType.CONTROL_MASK:
direction = event.get_scroll_deltas()[2]
if direction > 0: # scrolling down -> zoom out
self.set_zoom_level(self.get_zoom_level() - 0.1)
else:
self.set_zoom_level(self.get_zoom_level() + 0.1)
参考:GDK signal, keypress, and key masks