简体   繁体   English

如何清除 tkinter 中的 FigureCanvasTkAgg canvas

[英]how to clear a FigureCanvasTkAgg canvas in tkinter

I am trying to delete the graph on the canvas in the frame and leaving graph the graph frame.我正在尝试删除框架中 canvas 上的图形,并将图形留在图形框架中。 I have read that the canvas.delete('all') method should work but it does nothing when I use it, could somebody point out what i am doing wrong with this code or a better way of doing this please.I have used graph_frame.destroy() but that gets rid of the frame and the graph.我已经读过 canvas.delete('all') 方法应该可以工作,但是当我使用它时它什么也没做,有人可以指出我在此代码中做错了什么或更好的方法。我使用了 graph_frame .destroy() 但这摆脱了框架和图形。 My code is below, thanks for any help我的代码如下,感谢您的帮助

## code below
import tkinter
from tkinter import *

import numpy as np
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

my_window = tkinter.Tk()
my_window.geometry('1000x1000')
my_window.resizable(height=False, width=False)
current_list = ['Blue', 'Green', 'Yellow']

window_frame = tkinter.LabelFrame(my_window, height=800, width=600, text='window_frame', labelanchor='n', bd='4')
window_frame.grid(row=0, column=0, padx='10', pady='10')

graph_frame = tkinter.LabelFrame(window_frame, height=600, width=600, text='graph_frame', labelanchor='n', bd='4')
graph_frame.grid(row=0, column=0, padx='10', pady='10')

fig = Figure(figsize=(5, 5), dpi=100)
ax = fig.add_subplot(111)

for item in current_list:
    x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[2, 3],
                      unpack=True, delimiter=',')
    ax.plot(x, y)

canvas = FigureCanvasTkAgg(fig, master=graph_frame)  # A tk.DrawingArea
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, graph_frame)
toolbar.update()


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)

    canvas.mpl_connect("key_press_event", on_key_press)


def _quit():
    my_window.quit()  # stops mainloop
    my_window.destroy()  # this is necessary on Windows to prevent
    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=my_window, text="Quit", command=_quit)
button.grid(row=2, column=1)

def _clear():
    canvas.delete('all')

    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=window_frame, text="Clear", command=_clear)
button.grid(row=2, column=2)

my_window.mainloop()
Blue_Test.csv
Name1,Name2,Name3,Name4,Name5
1,10,19,1000,Blue
2,20,20,1001,Blue
3,30,21,1002,Blue
4,40,24,1005,Blue
5,60,25,1006,Blue
Green_Test.csv
Name1,Name2,Name3,Name4,Name5
1,10,19,1000,Green
2,40,24,1005,Green
3,60,25,1006,Green
4,20,20,1001,Green
5,30,21,1002,Green
Yellow_Test.csv
Name1,Name2,Name3,Name4,Name5
1,10,19,1000,Yellow
2,20,20,1001,Yellow
3,30,21,1002,Yellow
4,40,24,1005,Yellow
5,60,25,1006,Yellow

You can try:你可以试试:

def _clear():
    for item in canvas.get_tk_widget().find_all():
       canvas.get_tk_widget().delete(item)

See here for more details: https://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.find_all-method有关详细信息,请参见此处: https://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.find_all-method

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

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