【问题标题】:Clearing and plotting mathplot figure on tkinter在 tkinter 中清除和绘制 matplotlib 图
【发布时间】:2021-04-09 04:41:41
【问题描述】:

我需要一些关于我当前代码的帮助。我想通过 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() ```

【问题讨论】:

  • 你只是想一键清除剧情吗?
  • 是的,在我点击“绘图”后,该图形应该出现在我创建的画布中的按钮上方(而不是在新画布窗口中的按钮下方)并且点击“清除”我希望拥有启动程序后再次清晰的画布

标签: python matplotlib tkinter tkinter-canvas


【解决方案1】:

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

要将数学绘图画布放置在 tkinter 画布上,只需将 master 设置为 canvas 对象 (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() 返回用于实现 FigureCanvasTkAgg 的 Tk 小部件,这意味着您还可以使用所有方法 帆布。所以,output.get_tk_widget().delete('all') 也可以使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 2015-07-17
    • 2021-12-27
    • 2021-10-06
    • 2022-08-13
    • 2018-01-30
    • 2018-04-27
    相关资源
    最近更新 更多