Reputation: 5553
I bind key_press events in Matplotlib. They are not triggered unless I click on the figure first. I want to force the keyboard focus to be on the axes when the mouse enters it.
I attempted to follow the guidance in this answer:
Matplotlib 'key_press_event' does not respond
I get an error:
AttributeError: 'Canvas' object has no attribute 'setFocus'
This is my event handler:
def on_enter(self, event):
# self.fig.canvas.setFocus()
event.canvas.setFocus()
I tried to reference the canvas in two ways: from the event or from my stored figure object. Both give the same error.
How do you set the focus in matplotlib?
System information:
I set the handler using this:
self.cid_enter = self.fig.canvas.mpl_connect('axes_enter_event', self.on_enter)
When I call dir on the Canvas object, it says these are the properties:
Canvas Methods:
[
'__annotations__'
'__class__'
'__del__'
'__delattr__'
'__dict__'
'__dir__'
'__doc__'
'__eq__'
'__format__'
'__ge__'
'__getattribute__'
'__getstate__'
'__gt__'
'__hash__'
'__init__'
'__init_subclass__'
'__le__'
'__lt__'
'__module__'
'__ne__'
'__new__'
'__reduce__'
'__reduce_ex__'
'__repr__'
'__setattr__'
'__setstate__'
'__sizeof__'
'__str__'
'__subclasshook__'
'__weakref__'
'_active_widgets'
'_add_notifiers'
'_all_trait_default_generators'
'_button'
'_call_widget_constructed'
'_closed'
'_comm_changed'
'_compare'
'_control_comm'
'_cross_validation_lock'
'_current_image_mode'
'_cursor'
'_data_url'
'_default_keys'
'_descriptors'
'_device_pixel_ratio'
'_dom_classes'
'_figure_label'
'_fix_ipython_backend2gui'
'_force_full'
'_gen_repr_from_keys'
'_get_embed_state'
'_get_trait_default_generator'
'_handle_control_comm_msg'
'_handle_custom_msg'
'_handle_key'
'_handle_message'
'_handle_mouse'
'_handle_msg'
'_handle_set_device_pixel_ratio'
'_holding_sync'
'_idle_draw_cntx'
'_image_mode'
'_instance_inits'
'_ipython_display_'
'_is_idle_drawing'
'_is_numpy'
'_is_saving'
'_key'
'_lastKey'
'_last_buff'
'_last_mouse_xy'
'_lastx'
'_lasty'
'_lock_property'
'_log_default'
'_message'
'_model_id'
'_model_module'
'_model_module_version'
'_model_name'
'_msg_callbacks'
'_notify_observers'
'_notify_trait'
'_png_is_old'
'_print_pil'
'_property_lock'
'_register_validator'
'_remove_notifiers'
'_repr_keys'
'_repr_mimebundle_'
'_rubberband_height'
'_rubberband_width'
'_rubberband_x'
'_rubberband_y'
'_send'
'_set_device_pixel_ratio'
'_should_send_property'
'_size'
'_states_to_send'
'_static_immutable_initial_values'
'_switch_canvas_and_return_print_method'
'_timer_cls'
'_trait_default_generators'
'_trait_from_json'
'_trait_notifiers'
'_trait_to_json'
'_trait_validators'
'_trait_values'
'_traits'
'_view_count'
'_view_module'
'_view_module_version'
'_view_name'
'_widget_construction_callback'
'_widget_types'
'add_class'
'add_traits'
'blit'
'blur'
'buffer_rgba'
'button_pick_id'
'button_press_event'
'button_release_event'
'callbacks'
'capture_scroll'
'class_own_trait_events'
'class_own_traits'
'class_trait_names'
'class_traits'
'close'
'close_all'
'close_event'
'comm'
'copy_from_bbox'
'cross_validation_lock'
'current_dpi_ratio'
'device_pixel_ratio'
'draw'
'draw_event'
'draw_idle'
'enter_notify_event'
'events'
'figure'
'filetypes'
'fixed_dpi'
'flush_events'
'focus'
'footer_visible'
'get_default_filename'
'get_default_filetype'
'get_diff_image'
'get_manager_state'
'get_renderer'
'get_state'
'get_supported_filetypes'
'get_supported_filetypes_grouped'
'get_view_spec'
'get_width_height'
'grab_mouse'
'handle_ack'
'handle_button_press'
'handle_button_release'
'handle_comm_opened'
'handle_control_comm_opened'
'handle_dblclick'
'handle_draw'
'handle_event'
'handle_figure_enter'
'handle_figure_leave'
'handle_key_press'
'handle_key_release'
'handle_motion_notify'
'handle_refresh'
'handle_resize'
'handle_scroll'
'handle_send_image_mode'
'handle_set_device_pixel_ratio'
'handle_set_dpi_ratio'
'handle_toolbar_button'
'handle_unknown_event'
'has_trait'
'header_visible'
'hold_sync'
'hold_trait_notifications'
'inaxes'
'is_saving'
'key_press_event'
'key_release_event'
'keys'
'layout'
'leave_notify_event'
'log'
'manager'
'manager_class'
'model_id'
'motion_notify_event'
'mouse_grabber'
'mpl_connect'
'mpl_disconnect'
'new_manager'
'new_timer'
'notify_change'
'observe'
'on_msg'
'on_trait_change'
'on_widget_constructed'
'open'
'pan_zoom_throttle'
'pick'
'pick_event'
'print_figure'
'print_jpeg'
'print_jpg'
'print_png'
'print_raw'
'print_rgba'
'print_tif'
'print_tiff'
'print_to_buffer'
'print_webp'
'release_mouse'
'remove_class'
'renderer'
'required_interactive_framework'
'resizable'
'resize'
'resize_event'
'restore_region'
'scroll_event'
'scroll_pick_id'
'send'
'send_binary'
'send_event'
'send_json'
'send_state'
'set_cursor'
'set_image_mode'
'set_state'
'set_trait'
'setup_instance'
'show'
'start_event_loop'
'stop_event_loop'
'supports_blit'
'switch_backends'
'syncing_data_url'
'tabbable'
'toolbar'
'toolbar_position'
'toolbar_visible'
'tooltip'
'tostring_argb'
'tostring_rgb'
'trait_defaults'
'trait_events'
'trait_has_value'
'trait_metadata'
'trait_names'
'trait_values'
'traits'
'unobserve'
'unobserve_all'
'widget_types'
'widgetlock'
'widgets'
]
Upvotes: 0
Views: 91
Reputation: 10173
It seems this depends on the type of backend you're using, so here are a few options:
event.canvas.manager.show()
event.canvas.manager.activateWindow(); event.canvas.manager.raise_()
event.canvas.focus
and see if it's a function you can call.Upvotes: 0