Python 在matplotlib图形中打开关联菜单

Python 在matplotlib图形中打开关联菜单,python,matplotlib,contextmenu,Python,Matplotlib,Contextmenu,我想在matplotlib图形中单击鼠标右键的时间和位置打开关联菜单。我想在按下按钮的坐标中打开关联菜单,并获取这些坐标 这是我目前的代码: classs Figure(QMainWindow): x1 = 0 #I made this variables to get the coordinates and use it in x2 = 0 # another methods too def __init__(self): #A lot of st

我想在matplotlib图形中单击鼠标右键的时间和位置打开关联菜单。我想在按下按钮的坐标中打开关联菜单,并获取这些坐标

这是我目前的代码:

classs Figure(QMainWindow):

  x1 = 0      #I made this variables to get the coordinates and use it in 
  x2 = 0      # another methods too

  def __init__(self):
    #A lot of stuff in here to create the figure


  def right_click_press(self, event):

    if event.button == 3:       #"3" is the right button

        print "you click the right button" 
        print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
        event.button, event.x, event.y, event.xdata, event.ydata)

        #Get the coordinates of the mouse click
        Figure.x1 = event.xdata
        Figure.y1 = event.ydata 

        #I create the action
        noteAction_2 = QAction(QIcon(""), "Insert note",self, 
                                       triggered = self.openDialog)

        #I create the context menu
        self.popMenu = QMenu(self)
        self.popMenu.addAction(noteAction_2)

        cursor = QCursor()
        print cursor.pos()
        #self.connect(self.figure_canvas, SIGNAL("clicked()"), self.context_menu)
        #self.popMenu.exec_(self.mapToGlobal(event.globalPos()))

        self.popMenu.exec_() 

  def context_menu(self):
    pass
我尝试了
globalPos
mapToGlobal
pos
,并尝试了另一种方法(如上所示)在我单击的位置打开它,但没有得到我想要的结果。这就是我得到的:


我已经解决了替换线路的问题:

self.popMenu.exec()

为此:

    #I create the context menu
    self.popMenu = QMenu(self)
    self.popMenu.addAction(noteAction_2)

    cursor = QCursor()
    self.popMenu.popup(cursor.pos())
通过这样做,我放弃使用另一种方法,我很容易得到坐标。 希望这对你有帮助