How to display an image/screenshot in a Python Tkinter window without saving it?

Tkinter is a standard Python library that is used to create and develop GUI-based applications. To display an image, we use the PIL or Pillow library.

Let us suppose that we want to create an application that will take a screenshot of the window and display the captured image in another window. To achieve this, we can follow the steps given below −

  • Import the required libraries.

  • Create a universal button to take the screenshot.

  • Define a function to take the screenshot.

  • In the given function, define the coords and region through which we want to take the screenshot.

  • Create a Toplevel window and define a label image in it.

  • Pack the widget and display the output image.


# Import the required libraries
from tkinter import *
import pyautogui
from PIL import ImageTk, Image

# Create an instance of tknter frame or window
win = Tk()

# Set the size of the window

# Define a function to take the screenshot
def take_screenshot():
   x = 500
   y = 500
   # Take the screenshot in the given corrds
   im1 = pyautogui.screenshot(region=(x, y, 700, 300))

   # Create a toplevel window
   top = Toplevel(win)
   im1 = ImageTk.PhotoImage(im1)

   # Add the image in the label widget
   image1 = Label(top, image=im1)
   image1.image = im1, y=0)

Button(win, text='Take ScreenShot', command=take_screenshot).pack(padx=10, pady=10)



When we run the code, it will display a window with a button to take a screenshot.

Now, click the button "Take ScreenShot" and it will capture the screen of size 700px wide and 300 px height, starting from the coordinates (x=500, y=500).