如何使 Tkinter GUI 只绘制一次 Matplotlib 图

How to make Tkinter GUI only plot Matplotlib figure once

我有一些 Tkinter 代码可以制作 4tabbed gui。在 GUI tab2 中有一个按钮可以绘制一个简化的图形,它可以工作,但是当我再次单击该按钮时,它会重新打印 canvas 和第一个图正下方的图形。我研究了如何使用“关闭”或“清除”按钮删除绘图,但确实需要它只绘图一次(即使再次单击按钮)。

代码

import tkinter as tk                     
from tkinter import ttk 
import matplotlib
matplotlib.use("TkAgg")  # this is the backend of matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style


root = tk.Tk() 
root.title("Tab Widget")
root.geometry("1300x950") 
tabControl = ttk.Notebook(root) 
  
tab1 = ttk.Frame(tabControl) 
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl) 
tab4 = ttk.Frame(tabControl) 
  
tabControl.add(tab1, text ='Tab 1') 
tabControl.add(tab2, text ='Tab 2')
tabControl.add(tab3, text ='Tab 3')
tabControl.add(tab4, text ='Tab 4')


tabControl.pack(expand = 1, fill ="both") 
  
ttk.Label(tab1, text ="This is a  tab\n (1)").grid(column = 0, row = 0, padx = 30, pady = 30)   
ttk.Label(tab2, text ="This is another tab\n (2)").grid(column = 0, row = 0,  padx = 30, pady = 30) 
ttk.Label(tab3, text ="This is another tab\n (3)").grid(column = 0, row = 0,  padx = 30, pady = 30) 
ttk.Label(tab4, text ="This is another tab\n (4)").grid(column = 0, row = 0,  padx = 30, pady = 30)  


label_b1 = ttk.Label(tab1, text = "Button label 0,1").grid(column = 0, row = 1, padx = 30, pady = 30)
label_b1 = ttk.Label(tab1, text = "Button label 1,1").grid(column = 1, row = 1, padx = 30, pady = 30)
label_b1 = ttk.Label(tab1, text = "Button label 1,2").grid(column = 2, row = 1, padx = 30, pady = 30)


def plot(): 
    # the figure that will contain the plot 
    fig = Figure(figsize = (5, 5), dpi = 100) 
     
    y = [i**2 for i in range(101)] # list of squares
    
    # adding the subplot 
    plot1 = fig.add_subplot(111) 
    # plotting the graph 
    plot1.plot(y) 
    canvas = FigureCanvasTkAgg(fig, master = tab2)  # creating the Tkinter canvas containing the Matplotlib figure  
    canvas.draw() 
    canvas.get_tk_widget().grid()  # placing the canvas on the Tkinter window 


plot_button = tk.Button(master = tab2,  command = plot, height = 2,  width = 10, text = "Plot") 
plot_button.grid()   # place the button in main window 
root.mainloop() 

期望输出

第二次按下“绘图”按钮不会导致图表出现在第一个绘图的下方。

想法来自 xszym

was_plotted = False

def plot():
    global was_plotted
    if(was_plotted) == False:
            # the figure that will contain the plot 
        fig = Figure(figsize = (5, 5), dpi = 100) 
        
        y = [i**2 for i in range(101)] # list of squares
        
        # adding the subplot 
        plot1 = fig.add_subplot(111) 
        # plotting the graph 
        plot1.plot(y) 
        canvas = FigureCanvasTkAgg(fig, master = tab2)  # creating the Tkinter canvas containing the Matplotlib figure  
        canvas.draw() 
        canvas.get_tk_widget().grid()  # placing the canvas on the Tkinter window 

        
        was_plotted = True

你可以通过设置全局标志来解决它was_plotted

was_plotted = False

def plot():
    global was_plotted
    if(was_plotted):
        return 
    was_plotted = True