如何解决尝试嵌入 TkInter 时,Matplotlib 3D 绘图要么不具有交互性,要么在附加窗口中
我更新了我的问题:
在尝试了很多针对 SO 的类似问题的答案失败后,我现在希望有人能给我一个提示,说明为什么这不起作用:
我正在尝试将交互式 Matplotlib 3D 绘图嵌入到 TkInter 窗口中。我想要的是一个带有“绘图”按钮的 tk 窗口,上面有嵌入的交互式图形,没有其他窗口。
这是一个最低限度的可重现示例:
import tkinter as tk
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import *
def plot():
fig = plt.figure(figsize=[10,10],num="test")
# create the Tkinter canvas
# containing the Matplotlib figure
canvas = figureCanvasTkAgg(fig,master = window)
canvas.get_tk_widget().pack()
canvas.draw()
# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas,window)
toolbar.update()
# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()
ax = plt.axes(projection='3d')
ax.scatter(2,3,4)
window = tk.Tk()
# button that displays the plot
plot_button = tk.Button(master = window,command = plot,height = 2,width = 10,text = "Plot")
# place the button into the window
plot_button.pack()
# run the gui
window.mainloop()
我现在已经编辑了我的代码,因此 ax = plt.axes(projection='3d') 和 ax.scatter(2,4) 都在我的函数末尾。
现在我在 tkinter 窗口中有绘图并且它是交互式的。但是,每次按“绘图”时,我仍然会得到一个由 matplotlib 创建的新窗口。我还没有找到禁用此功能的方法。
我仍然对这里发生的事情感到困惑,希望有人能帮助我!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。