How to Embed Cartopy in a Python Tkinter GUI? How to Embed Cartopy in a Python Tkinter GUI? tkinter tkinter

How to Embed Cartopy in a Python Tkinter GUI?


There is nothing special about Cartopy and pyplot - everything will work with matplotlib's OO interface too. This means that embedding a Cartopy axes is as difficult as embedding a matplotlib axes in any of the GUI toolkits.

In this case, I've taken the example from http://matplotlib.org/examples/user_interfaces/embedding_in_tk2.html and simply added the following lines:

# Use add_axes or add_subplot.ax = fig.add_axes([0.01, 0.01, 0.98, 0.98],                   projection=ccrs.InterruptedGoodeHomolosine())# ax = fig.add_subplot(1, 1, 1, projection=ccrs.InterruptedGoodeHomolosine())ax.set_global()ax.stock_img()ax.coastlines()

My full application then looks like:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAggfrom matplotlib.figure import Figureimport cartopy.crs as ccrsimport sysimport Tkinter as Tkroot = Tk.Tk()root.wm_title("Cartopy in TK")fig = Figure(figsize=(8,4), dpi=100)ax = fig.add_axes([0.01, 0.01, 0.98, 0.98],                    projection=ccrs.InterruptedGoodeHomolosine())ax.set_global()ax.stock_img()ax.coastlines()ax.set_title('Cartopy and Tkinter')# a tk.DrawingAreacanvas = FigureCanvasTkAgg(fig, master=root)canvas.show()canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)button = Tk.Button(master=root, text='Quit', command=sys.exit)button.pack(side=Tk.BOTTOM)Tk.mainloop()

To result in a Tkinter GUI application that looks like:Tkinter screenshot