简体   繁体   English

在matplotlib图中打开上下文菜单

[英]Open a context menu in a matplotlib figure

I would like to open aa context menu in a matplotlib figure, at the time and position that I click the right mouse button in the figure. 我想在matplotlib图中打开上下文菜单,在该时间和位置上,单击图中的鼠标右键。 I want to open it in the coordinates where I press the button, and get those coordinates. 我想在按下按钮的坐标中将其打开,然后获取这些坐标。

This is my code so far: 到目前为止,这是我的代码:

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

I tried globalPos , mapToGlobal , pos and trying to make another method (as you can see above) to get to open it where I make the click, but I do not get the result that I want. 我尝试了globalPosmapToGlobalpos并尝试创建另一种方法(如您在上面看到的那样)以在单击时打开它,但是没有得到想要的结果。 This is what I get: 这是我得到的:

在此处输入图片说明

I already solved it replacing the line: 我已经解决了替换行:

self.popMenu.exec_()

with this: 有了这个:

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

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

By doing this, i discard using another method and i get the coordinates easily. 通过这样做,我放弃了使用另一种方法,并且很容易获得坐标。 Hope this help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM