尝试嵌入 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 (将#修改为@)