マウスがピクセルの上に置かれたときにウィンドウラベルのカーソル位置も更新する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')