import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

def on_clicked(*args):
    window = Gtk.Window()
    device = Gtk.get_current_event_device()
    assert device is not None

    window.realize()
    associated_device = device.get_associated_device()
    assert associated_device is not None

    Gtk.device_grab_add(window, associated_device, True)

    status = associated_device.grab(
        window.get_window(), Gdk.GrabOwnership.WINDOW, True,
        0, None, 0)
    return

w = Gtk.Window()
b = Gtk.Button(label="click to crash")
b.connect("clicked", on_clicked)
w.add(b)
w.show()
w.connect("destroy", Gtk.main_quit)
w.show_all()
Gtk.main()
