Skip to content

Commit 3d1be34

Browse files
committed
example working with Tk
1 parent a99367f commit 3d1be34

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed
+18-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import matplotlib
2-
# matplotlib.use('GTK3Cairo')
3-
matplotlib.use('TkAGG')
2+
matplotlib.use('GTK3Cairo')
3+
# matplotlib.use('TkAGG')
44
matplotlib.rcParams['toolbar'] = 'navigation'
55
import matplotlib.pyplot as plt
66
from matplotlib.backend_tools import ToolBase
77

88

9-
#Create a simple tool to list all the tools
9+
# Create a simple tool to list all the tools
1010
class ListTools(ToolBase):
11-
#keyboard shortcut
11+
# keyboard shortcut
1212
keymap = 'm'
13-
#Name used as id, must be unique between tools of the same navigation
13+
# Name used as id, must be unique between tools of the same navigation
1414
name = 'List'
1515
description = 'List Tools'
16-
#Where to put it in the toolbar, -1 = at the end, None = Not in toolbar
16+
# Where to put it in the toolbar, -1 = at the end, None = Not in toolbar
1717
position = -1
1818

1919
def trigger(self, event):
20-
#The most important attributes are navigation and figure
20+
# The most important attributes are navigation and figure
2121
self.navigation.list_tools()
2222

2323

24-
#A simple example of copy canvas
25-
#ref: at https://github.com/matplotlib/matplotlib/issues/1987
26-
class CopyTool(ToolBase):
24+
# A simple example of copy canvas
25+
# ref: at https://github.com/matplotlib/matplotlib/issues/1987
26+
class CopyToolGTK3(ToolBase):
2727
keymap = 'ctrl+c'
2828
name = 'Copy'
2929
description = 'Copy canvas'
30-
position = -1
30+
# It is not added to the toolbar as a button
31+
position = None
3132

3233
def trigger(self, event):
3334
from gi.repository import Gtk, Gdk
@@ -41,13 +42,12 @@ def trigger(self, event):
4142
fig = plt.figure()
4243
plt.plot([1, 2, 3])
4344

44-
#If we are in the old toolbar, don't try to modify it
45-
if matplotlib.rcParams['toolbar'] in ('navigation', 'None'):
46-
##Add the custom tools that we created
47-
fig.canvas.manager.navigation.add_tool(ListTools)
48-
fig.canvas.manager.navigation.add_tool(CopyTool)
45+
# Add the custom tools that we created
46+
fig.canvas.manager.navigation.add_tool(ListTools)
47+
if matplotlib.rcParams['backend'] == 'GTK3Cairo':
48+
fig.canvas.manager.navigation.add_tool(CopyToolGTK3)
4949

50-
##Just for fun, lets remove the back button
51-
fig.canvas.manager.navigation.remove_tool('Back')
50+
# Just for fun, lets remove the back button
51+
fig.canvas.manager.navigation.remove_tool('Back')
5252

5353
plt.show()

0 commit comments

Comments
 (0)