在 tkinter 上清除和绘制 mathplot 图 [英] Clearing and plotting mathplot figure on tkinter

查看:109
本文介绍了在 tkinter 上清除和绘制 mathplot 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. This point I reached yet. My problem is that I want to clear this canvas by hitting a button. To clear a canvas I suppose to initialize this before I can fill it with the plot.

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

在下面,您可以找到一个显示我的艺术水平的小代码.

Below you can find a small code that shows my state of arts.

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

推荐答案

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

There is no direct way to clear the figure from the math 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).

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

To place math plot canvas on tkinter canvas just set master as 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()

here the output is your FigureCanvasTkAgg instance for anyone else looking to achieve this. 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()

更新 output.get_tk_widget()返回用于实现FigureCanvasTkAgg的Tk小部件,这意味着您还可以使用以下所有方法帆布.因此, output.get_tk_widget().delete('all')也可以正常工作

update output.get_tk_widget() Return the Tk widget used to implement FigureCanvasTkAgg which means you can also use all the methods of canvas. So,output.get_tk_widget().delete('all') works as well

这篇关于在 tkinter 上清除和绘制 mathplot 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆