如何在 tkinter 中嵌入 matplotlib 图时获得 (3d) 交互性 canvas
How to get (3d) interactivity to work while embedding a matplotlib figure in a tkinter canvas
我已经在 tkinter gui canvas 中嵌入了一个 3D matplotlib 图,但无法使用(鼠标)交互(rotate/zoom 等)。如果我只是使用“pyplot.show()”命令而不嵌入 tk 交互性有效,我是否必须手动设置所有回调才能使用 tkinter 嵌入或是否有简单的方法?
显示立方体的简单示例脚本:
import tkinter as tk
from tkinter.ttk import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib import pyplot
from mpl_toolkits import mplot3d
import numpy
from stl import mesh
tk_root = tk.Tk()
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
data['vectors'][0] = numpy.array([[0, 1, 1],[1, 0, 1],[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],[0, 1, 1],[1, 1, 1]])
data['vectors'][2] = numpy.array([[1, 0, 0],[1, 0, 1],[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],[1, 0, 1],[1, 1, 0]])
data['vectors'][4] = numpy.array([[0, 0, 0],[1, 0, 0],[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],[0, 0, 1],[1, 0, 1]])
msh = mesh.Mesh(data)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(msh.vectors))
# pyplot.show()
canvas = FigureCanvasTkAgg(figure, tk_root)
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, tk_root)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
tk_root.mainloop()
必须 re-bind/connect 从 canvas 对象到图形坐标轴对象的交互回调
canvas.mpl_connect('button_press_event', view.axes._button_press)
canvas.mpl_connect('button_release_event', view.axes._button_release)
canvas.mpl_connect('motion_notify_event', view.axes._on_move)
如本例中所做的那样:
https://github.com/precise-simulation/mesh-viewer/blob/master/meshviewer_mpl_tk.py#L296-L298
接受的答案是您问题的有效解决方案,但我建议不要将 pyplot 与 tkinter 一起使用,因为它会导致我自己不理解的各种问题(例如:关闭 tkinter window 将离开 pyplot 运行)。相反,我会做这样的事情:
import tkinter as tk
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
X=np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]])
Y=np.array([[2, 2, 2], [1, 1, 1], [0, 0, 0]])
tk_root = tk.Tk()
figure = Figure(figsize=(12, 8))
ax= figure.add_subplot(1, 1, 1, projection='3d')
ax.plot_surface(data, X, Y, shade=True)
canvas = FigureCanvasTkAgg(figure, tk_root)
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
canvas.mpl_connect('button_press_event', ax._button_press)
canvas.mpl_connect('button_release_event', ax._button_release)
canvas.mpl_connect('motion_notify_event', ax._on_move)
tk_root.mainloop()
我知道这个答案并不完全符合你问题的背景,但这是总体思路
我已经在 tkinter gui canvas 中嵌入了一个 3D matplotlib 图,但无法使用(鼠标)交互(rotate/zoom 等)。如果我只是使用“pyplot.show()”命令而不嵌入 tk 交互性有效,我是否必须手动设置所有回调才能使用 tkinter 嵌入或是否有简单的方法?
显示立方体的简单示例脚本:
import tkinter as tk
from tkinter.ttk import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib import pyplot
from mpl_toolkits import mplot3d
import numpy
from stl import mesh
tk_root = tk.Tk()
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
data['vectors'][0] = numpy.array([[0, 1, 1],[1, 0, 1],[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],[0, 1, 1],[1, 1, 1]])
data['vectors'][2] = numpy.array([[1, 0, 0],[1, 0, 1],[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],[1, 0, 1],[1, 1, 0]])
data['vectors'][4] = numpy.array([[0, 0, 0],[1, 0, 0],[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],[0, 0, 1],[1, 0, 1]])
msh = mesh.Mesh(data)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(msh.vectors))
# pyplot.show()
canvas = FigureCanvasTkAgg(figure, tk_root)
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, tk_root)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
tk_root.mainloop()
必须 re-bind/connect 从 canvas 对象到图形坐标轴对象的交互回调
canvas.mpl_connect('button_press_event', view.axes._button_press)
canvas.mpl_connect('button_release_event', view.axes._button_release)
canvas.mpl_connect('motion_notify_event', view.axes._on_move)
如本例中所做的那样:
https://github.com/precise-simulation/mesh-viewer/blob/master/meshviewer_mpl_tk.py#L296-L298
接受的答案是您问题的有效解决方案,但我建议不要将 pyplot 与 tkinter 一起使用,因为它会导致我自己不理解的各种问题(例如:关闭 tkinter window 将离开 pyplot 运行)。相反,我会做这样的事情:
import tkinter as tk
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
X=np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]])
Y=np.array([[2, 2, 2], [1, 1, 1], [0, 0, 0]])
tk_root = tk.Tk()
figure = Figure(figsize=(12, 8))
ax= figure.add_subplot(1, 1, 1, projection='3d')
ax.plot_surface(data, X, Y, shade=True)
canvas = FigureCanvasTkAgg(figure, tk_root)
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
canvas.mpl_connect('button_press_event', ax._button_press)
canvas.mpl_connect('button_release_event', ax._button_release)
canvas.mpl_connect('motion_notify_event', ax._on_move)
tk_root.mainloop()
我知道这个答案并不完全符合你问题的背景,但这是总体思路