- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 win.geometry("700x350") # 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 image1.place(x=0, y=0) Button(win, text='Take ScreenShot', command=take_screenshot).pack(padx=10, pady=10) win.mainloop()
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).
- How to take a screenshot of the window using Python?(Tkinter)
- Tkinter-How to get the current date to display in a tkinter window?
- Read an image with OpenCV and display it with Tkinter
- How do I insert a JPEG image into a Python Tkinter window?
- How to get the current date to display in a Tkinter window?
- How to copy from clipboard using tkinter without displaying a window
- How can I display an image using Pillow in Tkinter?
- How to resize the background image to window size in Tkinter?
- How to center an image in canvas Python Tkinter
- How to remove the title bar in a Tkinter window without using overrideredirect() method?
- How to set the position of a Tkinter window without setting the dimensions?
- How to press a button without touching it on Tkinter?
- How to add an image in Tkinter?
- Creating a Frameless window in Python Tkinter
- Creating a Transparent window in Python Tkinter