框架不会在函数之前被销毁

Frame Won't Get Destroyed Before A Function

我正在尝试获取 tkinter canvas。 我在运行下面功能的框架内有一个按钮 我想在保存 canvas 之前立即销毁框架,但我做不到 这是我的尝试方式:

from tkinter import *
from PIL import ImageGrab

root = Tk()

canvas = Canvas(root,width=root.winfo_screenwidth(),height=root.winfo_screenheight(), bg="white",highlightthickness=0,cursor='dot')
canvas.pack(side=RIGHT,fill=Y)

save_canvas_frame = Frame(root,width=84,height=root.winfo_screenheight(),bg="#EFEFEF")
save_canvas_frame.place(x=0,y=0)

def save():
   # Close the frame
   save_canvas_frame.destroy()
   # Run the rest of the function
   x=root.winfo_rootx()+canvas.winfo_x()
   y=root.winfo_rooty()+canvas.winfo_y()
   x1=x+canvas.winfo_width()
   y1=y+canvas.winfo_height()   
   pic= ImageGrab.grab((x, y, x1, y1))
   pic.save("pic.png")

save_btn = Button(save_canvas_frame,text="Save Canvas",command=save)
save_btn.place(x=0,y=0)


root.mainloop()

一种可能的解决方案是创建 2 个函数并通过 .after():

调用其中一个
def save_img():
   x=canvas.winfo_x()
   y=canvas.winfo_y()
   
   x1=x+canvas.winfo_width()
   y1=y+canvas.winfo_height()   
   print(x,y,x1,y1)
   pic= ImageGrab.grab((x, y, x1, y1))
   pic.save("pic.png")


def save():
   # Close the frame
   
   save_canvas_frame.destroy()
   root.after(1,save_img)