在tkinter中清除嵌入的matplotlib条形图,可以通过以下步骤实现:
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
data = [1, 2, 3, 4, 5]
ax.bar(range(len(data)), data)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def clear_chart():
ax.clear()
canvas.draw()
clear_button = tk.Button(root, text="Clear Chart", command=clear_chart)
clear_button.pack()
tk.mainloop()
这样,当点击"Clear Chart"按钮时,条形图将被清除。
领取专属 10元无门槛券
手把手带您无忧上云