Saturday, 21 May 2022

Context Menu - Activated by Right Clicking

The script shows how to create a context menu in a GTK3 app. Users can activate the menu by right-clicking an item in a treeview.



  1. Define a treeview list with two columns.
  2. For the treeview, call the handler self.RowClick_event when the event button-press-event is trigged by the user clicking either the left or right mouse button.
  3. In the handler RowClick_event(self, widget, event=None), use event.button1 to detect a left-click (normal click) and event.button1 to detect a right-click (activating the context menu).
  4. Define a context menu and attach it to the treeview by the following:
    self.ctxmenu.attach_to_widget(self.tview,None)
    Three menu items, namely Move up, Move down and Delete, are defined in the menu. The actual codes for these items are given in self.callback. A separator is also defined to separate the Delete option from others.
  5. Define a confirmation dialog for the delete operation issued by the context menu.

Source Code


No comments:

Post a Comment