简体   繁体   English

在 tkinter 上清除和绘制 mathplot 图形

[英]Clearing and plotting mathplot figure on tkinter

I need some help for my current code.我需要一些关于我当前代码的帮助。 I want to create a window by tkinter and show a plot in a canvas I created by matplotlib before. I want to create a window by tkinter and show a plot in a canvas I created by matplotlib before. This point I reached yet.这一点我还没有达到。 My problem is that I want to clear this canvas by hitting a button.我的问题是我想通过点击一个按钮来清除这个 canvas。 To clear a canvas I suppose to initialize this before I can fill it with the plot.要清除 canvas,我想先初始化它,然后才能用 plot 填充它。

So my question is: How can I fill a plot in a created canvas?所以我的问题是:如何在创建的 canvas 中填写 plot?

Below you can find a small code that shows my state of arts.您可以在下面找到一个小代码,显示我的 state 的艺术。

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

There is no direct way to clear the figure from the math plot canvas.没有直接的方法可以从数学 plot canvas 中清除该数字。 So you can instead clear the canvas by destroying the widget itself using destroy method of tkinter canvas (note you cannot destroy the mathplot canvas itself as it doesn't have any methods such as destroy). So you can instead clear the canvas by destroying the widget itself using destroy method of tkinter canvas (note you cannot destroy the mathplot canvas itself as it doesn't have any methods such as destroy).

To place math plot canvas on tkinter canvas just set master as canvas object ( output = FigureCanvasTkAgg(fig, master = canvas) ) To place math plot canvas on tkinter canvas just set master as canvas object ( output = FigureCanvasTkAgg(fig, master = canvas) )

(Here is your corrected code) (这是您更正的代码)

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

Or you could use或者你可以使用

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

here the output is your FigureCanvasTkAgg instance for anyone else looking to achieve this.这里 output 是您的FigureCanvasTkAgg实例,供其他希望实现此目标的人使用。 And you just want to temporarily hide the plot output.get_tk_widget().pack_forget() and to display it again output.get_tk_widget().pack()而您只想暂时隐藏 plot output.get_tk_widget().pack_forget()并再次显示它output.get_tk_widget().pack()

update output.get_tk_widget() Return the Tk widget used to implement FigureCanvasTkAgg which means you can also use all the methods of canvas.更新output.get_tk_widget()返回用于实现 FigureCanvasTkAgg 的 Tk 小部件,这意味着您还可以使用 canvas 的所有方法。 So, output.get_tk_widget().delete('all') works as well因此, output.get_tk_widget().delete('all')也可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM