在 matplotlib 图形中打开上下文菜单
Open a context menu in a matplotlib figure
我想在 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())
通过这样做,我放弃了使用另一种方法,我很容易得到坐标。
希望对你有帮助。
我想在 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())
通过这样做,我放弃了使用另一种方法,我很容易得到坐标。 希望对你有帮助。