How to show webcam in TkInter Window - Python
Last Updated :
28 Apr, 2025
Python offers various modules for creating GUI applications, out of which, Tkinter lets users do various tasks inside their app. Thus, while creating a GUI app, have you ever felt the need to let the user open the camera on a specific condition? Don't know, how to achieve that. Continue reading this article further to know about how to open the camera in Tkinter.
Modules Required:
- Tkinter: As the GUI application is created in Python, then you surely need the Tkinter module. It is one of the easiest and fastest ways of creating GUI applications. You can download the module Tkinter using the following command:
pip install tkinter
- OpenCV: The Python module which is used for image and video processing is known as OpenCV. The video capture objects can also be captured through this module. The OpenCV module can be installed using the following command:
pip install opencv-python
- Pillow: The module which uses histograms to extract some statistical data out of images is known as the Pillow module. The pillow module can be installed using the following command:
pip install pillow
Stepwise Implementation
Step 1: First of all, import the libraries, Tkinter, OpenCV, and PIL.Image and PIL.ImageTk. These are the libraries needed for opening the camera in your app.
Python3
from tkinter import *
import cv2
from PIL import Image, ImageTk
Step 2: In this step, we will get a video capture object for the camera.
Python3
cap = cv2.VideoCapture(0)
Step 3: Now, define the width and height in the variables and set it for the video capture object.
Python3
width, height = # Width of camera, #Height of Camera
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
Step 4: Then, create a GUI app and bind the GUI app with the Escape button, such that whenever the escape button is pressed, the app is closed.
Python3
app = Tk()
app.bind('<Escape>', lambda e: app.quit())
Step 5: Next, create a label to display on the app. This is the label on which the camera will be opened.
Python3
label_widget = Label(app)
label_widget.pack()
Step 6: Moreover, create a button that when pressed will open the camera on the app.
Python3
button1 = Button(app, text="Open Camera",
command=open_camera)
button1.pack()
Step 7: This is the final and main step which will open the camera. In this function first, we will capture the video frame by frame. Now, we need to set up the color space for the video. It can be achieved using the cvtColor() function and we will capture the latest frame and transform it into an image. And then we will convert the captured image to a photo image for displaying on the label on the GUI app.
Python3
def open_camera():
# Capture the video frame by frame
_, frame = vid.read()
# Convert image from one color space to other
opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# Capture the latest frame and transform to image
captured_image = Image.fromarray(opencv_image)
# Convert captured image to photoimage
photo_image = ImageTk.PhotoImage(image=captured_image)
# Displaying photoimage in the label
label_widget.photo_image = photo_image
# Configure image in the label
label_widget.configure(image=photo_image)
# Repeat the same process after every 10 seconds
label_widget.after(10, open_camera)
Example of How to Open Camera in Tkinter
In this example, we have placed a button 'Open Camera' on the GUI app. Whenever that button is clicked, the camera will be opened, while whenever the escape button is pressed, the camera will shut down. Below is the complete code in one place for the reader's ease so, that they can run the code and have some fun.
Python3
# Python program to open the
# camera in Tkinter
# Import the libraries,
# tkinter, cv2, Image and ImageTk
from tkinter import *
import cv2
from PIL import Image, ImageTk
# Define a video capture object
vid = cv2.VideoCapture(0)
# Declare the width and height in variables
width, height = 800, 600
# Set the width and height
vid.set(cv2.CAP_PROP_FRAME_WIDTH, width)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Create a GUI app
app = Tk()
# Bind the app with Escape keyboard to
# quit app whenever pressed
app.bind('<Escape>', lambda e: app.quit())
# Create a label and display it on app
label_widget = Label(app)
label_widget.pack()
# Create a function to open camera and
# display it in the label_widget on app
def open_camera():
# Capture the video frame by frame
_, frame = vid.read()
# Convert image from one color space to other
opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# Capture the latest frame and transform to image
captured_image = Image.fromarray(opencv_image)
# Convert captured image to photoimage
photo_image = ImageTk.PhotoImage(image=captured_image)
# Displaying photoimage in the label
label_widget.photo_image = photo_image
# Configure image in the label
label_widget.configure(image=photo_image)
# Repeat the same process after every 10 seconds
label_widget.after(10, open_camera)
# Create a button to open the camera in GUI app
button1 = Button(app, text="Open Camera", command=open_camera)
button1.pack()
# Create an infinite loop for displaying app on screen
app.mainloop()
Output:
Similar Reads
How To Get Rid Of Python Tkinter Root Window? Python's Tkinter library offers a simple and effective way to create graphical user interfaces (GUIs) for your applications. However, when using Tkinter, you may encounter situations where you need to close or remove the root window, which is the main window of your GUI. Whether you're looking to cl
2 min read
How to close only the TopLevel window in Python Tkinter? In this article, we will be discussing how to close only the TopLevel window in Python Tkinter. Syntax: def exit_btn(): Â Â top.destroy() Â Â top.update() btn = Button(top,text='#Text of the exit button',command=exit_btn) Stepwise Implementation Step 1: First of all, import the library tkinter. from
3 min read
Hide and Unhide The Window in Tkinter - Python Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to
2 min read
How to use HTML in Tkinter - Python? Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to
2 min read
How To Center A Window On The Screen In Tkinter? Tkinter provides a basic geometry manager to control the placement of widgets within a window, it doesn't offer a built-in method to automatically center a window on the screen. However, achieving this task is relatively straightforward with a few lines of code. In this article, we'll explore differ
2 min read
How to resize Image in Python - Tkinter? Python provides multiple options for building GUI (Graphical User Interface) applications. Among them, Tkinter is one of the most widely used and simplest options. It comes bundled with Python and serves as a standard interface to the Tk GUI toolkit.However, Tkinter alone does not provide support fo
2 min read
How To Bind The Enter Key To A Tkinter Window? Tkinter is a Python library to develop GUI applications. Tkinter has many useful widgets that are necessary to build desktop applications. In this article, we will learn how to bind an Entry widget with a Tkinter Window. Binding Key in TkinterA key event occurs when the user clicks on any key on the
4 min read
How to Install Tk in Windows? In this article, we will look into the various methods of installing Tkinter on a Windows machine.Note : For Python 3 tkinter should already be included with your Python installation. However, if itâs not available or youâre encountering issues, you can install or reinstall it using the following st
2 min read
Save Image To File in Python using Tkinter Saving an uploaded image to a local directory using Tkinter combines the graphical user interface capabilities of Tkinter with the functionality of handling and storing images in Python. In this article, we will explore the steps involved in achieving this task, leveraging Tkinter's GUI features to
4 min read
How to add PDF in Tkinter GUI Python ? In this article, We are going to see how to add a PDF file Tkinter GUI, For that, we don't have a direct widget to do this. For that, We need to have python version 2.7 or more. And you need to install the 'tkPDFViewer' library. This library allows you to embed the PDF file in your Tkinter GUI. Inst
2 min read