使用 python GTK3 在 DrawArea 中获取鼠标滚轮事件

Getting mousewheel events in DrawArea with python GTK3

我正在尝试在 Gtk.DrawArea 中获取 mousewheel 事件。 有人知道如何实现这一目标吗?方法 当前从未调用 DrawTest.on_scroll():

#!/usr/bin/env python3                                                                                                                                                                   

import gi                                                                                                                                                                                
gi.require_version('Gtk', '3.0')                                                                                                                                                         
from gi.repository import Gtk                                                                                                                                                            

class DrawTest(Gtk.DrawingArea):                                                                                                                                                         
    def __init__(self):                                                                                                                                                                  
        super(DrawTest, self).__init__()                                                                                                                                                 
        self.connect("scroll-event", self.on_scroll)                                                                                                                                     

    def on_scroll(self, btn, event):                                                                                                                                                     
        print("Scroll event")                                                                                                                                                            
        return True                                                                                                                                                                      


class MainWindow(Gtk.Window):                                                                                                                                                            
    def __init__(self):                                                                                                                                                                  
        super(MainWindow, self).__init__()                                                                                                                                               
        self.connect("destroy", lambda x: Gtk.main_quit())                                                                                                                               

        evtest = DrawTest()                                                                                                                                                              

        self.add(evtest)                                                                                                                                                                 
        self.show_all()                                                                                                                                                                  

    def run(self):                                                                                                                                                                       
        Gtk.main()                                                                                                                                                                       

def main(args):                                                                                                                                                                          
    mainwdw = MainWindow()                                                                                                                                                               
    mainwdw.run()                                                                                                                                                                        
    return 0                                                                                                                                                                             

if __name__ == '__main__':                                                                                                                                                               
    import sys                                                                                                                                                                           
    sys.exit(main(sys.argv))

您需要设置或添加 Gtk.DrawingArea 应该处理的事件。

只需将这行代码添加到您的 DrawTest class 的 init 方法中:

self.set_events (Gdk.EventMask.ALL_EVENTS_MASK)

它应该是这样的:

class DrawTest(Gtk.DrawingArea): 
    def __init__(self): 
        super(DrawTest, self).__init__()
        self.set_events (Gdk.EventMask.ALL_EVENTS_MASK)
        self.connect("scroll-event", self.on_scroll)
    ...

set_events 方法来自 Gtk.Widget class 它说:

The event mask for a window determines which events will be reported for that window from all master input devices. For example, an event mask including Gdk.EventMask.BUTTON_PRESS_MASK means the window should report button press events. The event mask is the bitwise OR of values from the Gdk.EventMask enumeration.

See the ‘input handling overview [event-masks]’ for details.

为简单起见,我设置了 ALL_EVENTS_MASK, more on Gdk.EventMask

PS:请注意,Gdk.WindowGtk.Window 不同,如果您阅读更多关于该主题的内容,您会看到这一点。