I'm trying to make a screen shot of a window made with Tkinter, but when the window is not on the top level I just get what I see on the screen. I rather would like to have the screenshot of the window itself even if there some other windows on top of it.
Here's a minimal example to start with. I just made a function to save the screenshot with ImageGrab. Function that's called when I click on File.screenshot or when I click on the hello button. The funciton using a pause of 2 second, like that I have time to get another window on top of the Tkinter one.
What I would like at the end is an image of the Tkinter window instead of an image of my screen. for instance if I put another window on top I get:
from tkinter import *
from PIL import ImageGrab
import time
top = Tk()
top.geometry("600x400")
def helloCallBack():
time.sleep(2)
x = top.winfo_rootx()
y = top.winfo_rooty()
xx = x + top.winfo_width()
yy = y + top.winfo_height()
print(x,y,xx,yy)
ImageGrab.grab(bbox=(x, y, xx, yy)).save("screenImage2.jpg")
menuBar = Menu(top)
menuFile = Menu(menuBar, tearoff=0)
menuFile.add_command(label="Screenshot", command=helloCallBack)
menuBar.add_cascade( label="File", menu=menuFile)
top.config(menu = menuBar)
B = Button(top, text = "Hello", command = helloCallBack)
B.place(x = 50,y = 50)
top.mainloop()