2016-02-12 145 views
0

我想在matplotlib图中打开一个上下文菜单,在图中单击鼠标右键的时间和位置。我想在按下按钮的坐标中打开它,并获取这些坐标。在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 

我试过globalPosmapToGlobalpos并尝试进行另一种方法(因为你可以看到上面的)去打开它,我做了一下,但我没有得到我想要的结果。这就是我得到:

enter image description here

回答

1

我已经解决了它替换行:

self.popMenu.exec_()

与此:

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

    cursor = QCursor() 
    self.popMenu.popup(cursor.pos()) 

通过这样做,我放弃使用其他方法,我很容易获得坐标。 希望这对你有所帮助。

相关问题