0

I want to save a snapshot of my running GUI window written in Python-tkinter.

For example, saving the image of the window by a click on the "Save" button in the given code.

import tkinter as tk

def set_text(val):
    entry.delete(0,tk.END)
    entry.insert(0,val)
    return entry

def save_img():
    pass

root = tk.Tk()

entry = tk.Entry(root,width=10)
entry.pack()

b1 = tk.Button(root,text="animal",command=lambda:set_text("Bird"))
b1.pack()

b2 = tk.Button(root,text="plant",command=lambda:set_text("Seed"))
b2.pack()

b3 = tk.Button(root,text="Save",command=save_img)
b3.pack(pady=20)


root.mainloop()

Thanks!

Adam
  • 1
  • 1
  • 1
    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) – faressalem Apr 25 '20 at 05:33
  • You can try [ImageGrab](https://pillow.readthedocs.io/en/stable/reference/ImageGrab.html) from `Pillow` module. – acw1668 Apr 25 '20 at 11:52
  • @FaresSalem Thanks for comments but I didn't get what I wanted. I didn't implement the code but my understanding from your suggestions is that your solutions are taking a snapshot from the screen while what I am looking for is to take snapshot of the GUI window (regardless of its position/dimension on the screen). I am not a software developer, but since Python is generation the GUI window, there must be a way to capture that image without locating the window on the screen. Thanks! – Adam Apr 26 '20 at 17:06
  • @acw1668, please see my comment for FaresSalem. – Adam Apr 26 '20 at 17:07
  • @Adam I think you need first to locate the window, Python generates the window, yes, but it doesn't save continuously the location of the window on the screen. What saves the locations, is the [window manager](https://en.wikipedia.org/wiki/Window_manager) I think, and I don't know how to get this meta-data. – faressalem Apr 26 '20 at 17:45

1 Answers1

0

In the save_img method, you can find first the coordinates of the top window then pass them to the grab method in the Pyscreenshot module. How to find the coordinates? You can start from here:

Methods like winfo_x, winfo_y, and winfo_width might help you.

faressalem
  • 574
  • 6
  • 20