首页 > 解决方案 > 在 tkinter 上清除和绘制 mathplot 图形

问题描述

我需要一些关于我当前代码的帮助。我想通过 tkinter 创建一个窗口,并在我之前由 matplotlib 创建的画布中显示一个绘图。这一点我还没有达到。我的问题是我想通过点击一个按钮来清除这个画布。为了清除画布,我想先初始化它,然后才能用绘图填充它。

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

您可以在下面找到一个显示我的艺术状态的小代码。

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() ```

标签: pythonmatplotlibtkintertkinter-canvas

解决方案


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

要将数学绘图画布放在 tkinter 画布上,只需将 master 设置为canvasobject ( 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()

update output.get_tk_widget() 返回用于实现 FigureCanvasTkAgg 的 Tk 小部件,这意味着您还可以使用画布的所有方法。所以,output.get_tk_widget().delete('all')也可以


推荐阅读