I want to have a context menu when clicking with right mouse button.
There is an old PyGtk-specific question about it. But it uses a very old gtk version and deprecated functions.
I am unsure how PyGObject (Gtk 3.*) realize this. This is what I have tried so far:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.resize(100, 100)
self._build_context_menu()
# button event
self.connect('button-press-event', self._on_button_press_event)
# close event
self.connect('delete-event', self._on_delete_event)
def _build_context_menu(self):
self.cmenu = Gtk.Menu.new()
self.cm_item = Gtk.MenuItem.new_with_label('label')
self.cmenu.append(self.cm_item)
def _on_delete_event(self, a, b):
Gtk.main_quit()
def _on_button_press_event(self, widget, event):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
self.cmenu.popup_at_pointer()
return True # event was handled
if __name__ == '__main__':
window = MyWindow()
window.show_all()
Gtk.main()
The result of that code: When right click on the window something happens. A small white rectangle does appear. But it is empty.
You forgot to show the menu:
def _build_context_menu(self):
self.cmenu = Gtk.Menu.new()
self.cm_item = Gtk.MenuItem.new_with_label('label')
self.cmenu.append(self.cm_item)
self.cmenu.show_all()