0

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:enter image description here

   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()
ymmx
  • 4,769
  • 5
  • 32
  • 64
  • I get what I see on the screen. Did you have time to put another window on top of the Tkinter one? – ymmx Apr 07 '20 at 07:12
  • Does this answer your question? [How to do a screenshot of a tkinter application?](https://stackoverflow.com/questions/19964345/how-to-do-a-screenshot-of-a-tkinter-application) – stovfl Apr 07 '20 at 07:23
  • You can edit your post with your expectation output. – jizhihaoSAMA Apr 07 '20 at 07:30
  • 1
    You can only get a screenshot of what is visible on the screen — if you can get the screen coordinates of the window you can select the area of the screen that has the tkinter window in it. – martineau Apr 07 '20 at 07:37
  • It is possible with pyQt. With the `grab()` method. So I guessed it was also possible with Tk. – ymmx Apr 07 '20 at 07:52
  • How would you activate the screen capture function of your application when it is not the active window? – acw1668 Apr 07 '20 at 07:52
  • From a main window. Or with time delay.They are plenty way to trigger the screenshot. – ymmx Apr 07 '20 at 07:54
  • Read about this [quesiton](https://stackoverflow.com/questions/19695214/python-screenshot-of-inactive-window-printwindow-win32gui) – jizhihaoSAMA Apr 07 '20 at 08:01

0 Answers0