1

我试图在 matplotlib 画布上创建一个“右键单击”弹出菜单,当鼠标悬停在一个像素上时,它还会更新窗口标签上的光标位置。问题是,如果我右键单击,它会按预期显示菜单。然后在我将鼠标悬停或什至左键单击画布上的任意位置后立即进行选择后,弹出窗口会不断重新出现 - 就像我没有右键单击时再次右键单击一样。几乎就好像我必须刷新事件或将其状态重置为“未单击”我必须遵循代码。但我不知道如何使它工作。

如果悬停时我不更新 GUI 上的鼠标位置,它可以正常工作。但是,一旦我尝试进行动态更新,它就会工作,直到我第一次右键单击。

self.cidpress = self.ui.mplWidget.canvas.mpl_connect('motion_notify_event', self.get_mouse_coords)

def get_mouse_coords(self, event):
    """
    Updates the GUI with the mouse coordinates and the parameter value

    :param event: event handler from Matplotlib.
    """
    pos = QtGui.QCursor().pos()

    if event.button == 3:
        self.open_menu(pos)

    if isinstance(event.xdata, float) and isinstance(event.ydata, float):
        self.ui.lbl_x_pos.setText('X : ' + str(round(event.xdata, 3)))
        self.ui.lbl_y_pos.setText('Y : ' + str(round(event.ydata, 3)))
    try:
        self.ui.lbl_param_pos.setText('P :' +
                                      str(self.parameter_stats[self.output_parameter][int(event.xdata)][
                                          int(event.ydata)]))
    except:
        self.ui.lbl_param_pos.setText('P : ' + 'None')

def open_menu(self, position):
    """
    Open a menu to let the user to save the file or to fit a curve at this position

    :param position: QT mouse position instance
    """

    #======================================#
    #  Add menus to action list
    #======================================#
    menu = QtGui.QMenu()
    save_action = menu.addAction("Save Image"),
    fit_curve_action = menu.addAction("Fit Curve")

    #======================================#
    #  Bring up Action list at the mouse position and call the appropriate function
    #======================================#
    action = menu.exec_(self.tableWidget.mapFromGlobal(position))
    if action == save_action:
        self.gt = lib_hdf_image_stats.GeoTiffTools()
        try:
            self.gt.write_to_geotiff(self.parameter_stats[self.output_parameter], self.pos_crnrs,
                                     [self.dict_list[0].delta_lat, self.dict_list[0].delta_lon],
                                     str(self.output_file_name))
        except:
            self.lg.exception('Error saving file :: Check file name')
    elif action == fit_curve_action:
        print('!Fit action')
4

0 回答 0