8000 mod keypress in figuremanager · matplotlib/matplotlib@3118a5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3118a5a

Browse files
committed
mod keypress in figuremanager
1 parent 8cceed4 commit 3118a5a

File tree

3 files changed

+581
-1
lines changed

3 files changed

+581
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import matplotlib
2+
matplotlib.use('GTK3Cairo')
3+
matplotlib.rcParams['toolbar'] = 'navigation'
4+
import matplotlib.pyplot as plt
5+
from matplotlib.backend_tools import ToolBase
6+
7+
8+
#Create a simple tool to list all the tools
9+
class ListTools(ToolBase):
10+
#keyboard shortcut
11+
keymap = 'm'
12+
#Name used as id, must be unique between tools of the same navigation
13+
name = 'List'
14+
description = 'List Tools'
15+
#Where to put it in the toolbar, -1 = at the end, None = Not in toolbar
16+
position = -1
17+
18+
def activate(self, event):
19+
#The most important attributes are navigation and figure
20+
self.navigation.list_tools()
21+
22+
23+
#A simple example of copy canvas
24+
#ref: at https://github.com/matplotlib/matplotlib/issues/1987
25+
class CopyTool(ToolBase):
26+
keymap = 'ctrl+c'
27+
name = 'Copy'
28+
description = 'Copy canvas'
29+
position = -1
30+
31+
def activate(self, event):
32+
from gi.repository import Gtk, Gdk, GdkPixbuf
33+
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
34+
window = self.figure.canvas.get_window()
35+
x, y, width, height = window.get_geometry()
36+
pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)
37+
clipboard.set_image(pb)
38+
39+
40+
fig = plt.figure()
41+
plt.plot([1, 2, 3])
42+
43+
#If we are in the old toolbar, don't try to modify it
44+
if matplotlib.rcParams['toolbar'] in ('navigation', 'None'):
45+
##Add the custom tools that we created
46+
fig.canvas.manager.navigation.add_tool(ListTools)
47+
fig.canvas.manager.navigation.add_tool(CopyTool)
48+
49+
##Just for fun, lets remove the back button
50+
fig.canvas.manager.navigation.remove_tool('Back')
51+
52+
plt.show()

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,8 @@ def key_press(self, event):
26112611
Implement the default mpl key bindings defined at
26122612
:ref:`key-event-handling`
26132613
"""
2614-
key_press_handler(event, self.canvas, self.canvas.toolbar)
2614+
if rcParams['toolbar'] != 'navigation':
2615+
key_press_handler(event, self.canvas, self.canvas.toolbar)
26152616

26162617
def show_popup(self, msg):
26172618
"""

0 commit comments

Comments
 (0)
0