在 tkinter 上清除和绘制 mathplot 图

Clearing and plotting mathplot figure on tkinter

我的当前代码需要一些帮助。我想通过 tkinter 创建一个 window 并在我之前由 matplotlib 创建的 canvas 中显示一个图。这一点我还没有达到。我的问题是我想通过点击一个按钮来清除这个 canvas。要清除 canvas,我想在用绘图填充它之前对其进行初始化。

所以我的问题是:如何在创建的canvas中填充绘图?

您可以在下面找到一个展示我的技术水平的小代码。

from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)  

def plot(): 
    fig = Figure(figsize = (5, 5), dpi = 100)
    y = [i**2 for i in range(101)]

    # adding the subplot 
    plot1 = fig.add_subplot(111) 

    # plotting the graph 
    plot1.plot(y) 

    # creating the Tkinter canvas 
    # containing the Matplotlib figure 
    output = FigureCanvasTkAgg(fig, master = window)
    output.draw()

    # placing the canvas on the Tkinter window 
    output.get_tk_widget().pack() 

def clear_plot():
    canvas.delete('all') 

# the main Tkinter window 
window = Tk() 

# setting the title 
window.title('Plotting in Tkinter') 

# dimensions of the main window 
window.geometry("700x700") 

canvas = Canvas(window, width=500, height=500) 
canvas.pack()

# button that displays the plot 
plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "Plot") 

clear_button = Button(master = window, command = clear_plot, height = 2, width = 10, text = "clear", background = "yellow")

# place the button 
plot_button.pack() 
clear_button.pack()

# run the gui 
window.mainloop() ```

没有直接的方法可以从数学图中清除图形 canvas。因此,您可以通过使用 tkinter canvas 的 destroy 方法销毁小部件本身来清除 canvas (请注意,您不能销毁 mathplot canvas 本身,因为它没有任何destroy 等方法)。

要在 tkinter canvas 上放置数学绘图 canvas 只需将 master 设置为 canvas object (output = FigureCanvasTkAgg(fig, master = canvas))

(这是您更正后的代码)

from tkinter import *
from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)  

def plot():
    global output, fig
    
    fig = Figure(figsize = (5, 5), dpi = 100)
    y = [i**2 for i in range(101)]
    # adding the subplot 
    plot1 = fig.add_subplot(111) 

    # plotting the graph 
    plot1.plot(y) 

    # creating the Tkinter canvas 
    # containing the Matplotlib figure 
    output = FigureCanvasTkAgg(fig, master = canvas)
    output.draw()

    # placing the canvas on the Tkinter window 
    output.get_tk_widget().pack() 

def clear_plot():
    global output
    if output:
        for child in canvas.winfo_children():
            child.destroy()
        # or just use canvas.winfo_children()[0].destroy()  
  
    output = None

# the main Tkinter window 
window = Tk() 

output = None
fig = None

# setting the title 
window.title('Plotting in Tkinter') 

# dimensions of the main window 
window.geometry("700x700") 

canvas = Canvas(window, width=500, height=500, bg='white') 
canvas.pack()

# button that displays the plot 
plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "Plot") 

clear_button = Button(master = window, command = clear_plot, height = 2, width = 10, text = "clear", background = "yellow")

# place the button 
plot_button.pack() 
clear_button.pack()

# run the gui 
window.mainloop()

或者您可以使用

def clear_plot():
    global output
    if output:
        output.get_tk_widget().destroy()
    output = None

此处的输出是您的 FigureCanvasTkAgg 实例,供其他希望实现此目的的人使用。你只是想暂时隐藏情节 output.get_tk_widget().pack_forget() 并再次显示它 output.get_tk_widget().pack()

更新 output.get_tk_widget() Return 用于实现 FigureCanvasTkAgg 的 Tk 小部件,这意味着您还可以使用 canvas。所以,output.get_tk_widget().delete('all') 也有效