Can anyone help me to disable the context menu of pyqtgraph or remove some options from it?
4 Answers
I've been working on this myself and this is what I've found (as of May 2021). For reference, the image below is the right click menu that appears for the PyQtGraph PlotWidget, but not all of the items (QActions) are added by the PlotWidget itself.
The menu items above the separator are created by the ViewBox menu (source code here) which can be accessed through
PlotItem.vb
orPlotItem.getViewBox()
(if you have aPlotWidget
, you can get thePlotItem
throughPlotWidget.getPlotItem()
). The menu items (QAction
s) can be accessed throughPlotItem.vb.menu.actions()
(Qt reference here) and can be removed by checking theQAction.text()
to find theQAction
you want to change or remove.PlotItem
(source code here) creates a menu calledctrlMenu
, which contains the plot options (e.g. Transform, Downsample). This is the "Plot Options" submenu in the image above, and does not appear in thePlotItem.vb.menu.actions()
but can be accessed viaPlotItem.ctrlMenu.menuAction()
(StackOverflow reference).The "Export..." option comes from the underlying
GraphicsScene
(source code here) and can be accessed viaViewBox.scene().contextMenu[0]
which gives the "Export..." QAction.Any of the
QAction
can be hidden/shown byQAction.setVisible()
(Qt reference)

- 291
- 5
- 5
Use the PlotItem.setMenuEnabled method. Something like:
self.plot = pg.PlotItem()
self.plot.setMenuEnabled(False)
I have not found a way to remove options from it but perhaps it's possible. I would be interested in this as well.

- 5,376
- 2
- 24
- 43
Found a way to edit & remove the options, check this out: ViewBoxMenu
remove the Export... Options is found here: Export... (contextMenu)
i just cleared the list:
export = self.gui.Display.ui.graphicsView.sceneObj.contextMenu
del export[:]

- 25
- 4
In practice to implement Elliot's answer I would subclass Viewbox
, after the initialization you can hide the action of the default ViewBoxMenu
which you don't want to use. Then you can add your own actions to the self.menu
.
Below I hide the "X-axis" option and add a submenu to the default ViewBox
contextual menu to change the background color.
plot = pg.PlotWidget(title="New Menu",viewBox=NewViewBox())
class NewViewBox(pg.ViewBox):
def __init__(self,parent=None):
super(NewViewBox, self).__init__(parent)
self.myMenuEdit()
def myMenuEdit(self):
#Hide Default Actions
MenusToHide = ["X axis"] #Names of menus to hide
w = self.menu.actions()
for m in w:
for mhs in MenusToHide:
if (m.text().startswith(mhs)):
m.setVisible(False)
break
#AddMySubMenu
leftMenu = self.menu.addMenu("Background color")
group = QtGui.QActionGroup(self)
Yellow = QtGui.QAction(u'Yellow', group)
Red = QtGui.QAction(u'Red', group)
leftMenu.addActions(group.actions())
Yellow.setCheckable(True)
Red.setCheckable(True)
group.triggered.connect(self.setBgColor)
self.bgActions=[Yellow,Red]
def setBgColor(self, action):
mode = None
if action == self.bgActions[0]:
self.setBackgroundColor("y")
elif action == self.bgActions[1]:
self.setBackgroundColor("r")
Note that each parent of the ViewBox
which has a method getContextMenus
has its own menu options and they will be loaded and added to the menu at each click through the call of self.scene().addParentContextMenus(self, menu, ev)
in the function raiseContextMenu
.
PlotItem
, for example, adds Export and Plot Options. You can override the menus of each of those parental classes if needed.

- 2,154
- 1
- 16
- 36

- 31
- 5