How to show webcam in TkInter Window – Python
Last Updated :
26 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 =
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():
_, frame = vid.read()
opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
captured_image = Image.fromarray(opencv_image)
photo_image = ImageTk.PhotoImage(image = captured_image)
label_widget.photo_image = photo_image
label_widget.configure(image = photo_image)
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
from tkinter import *
import cv2
from PIL import Image, ImageTk
vid = cv2.VideoCapture( 0 )
width, height = 800 , 600
vid. set (cv2.CAP_PROP_FRAME_WIDTH, width)
vid. set (cv2.CAP_PROP_FRAME_HEIGHT, height)
app = Tk()
app.bind( '<Escape>' , lambda e: app.quit())
label_widget = Label(app)
label_widget.pack()
def open_camera():
_, frame = vid.read()
opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
captured_image = Image.fromarray(opencv_image)
photo_image = ImageTk.PhotoImage(image = captured_image)
label_widget.photo_image = photo_image
label_widget.configure(image = photo_image)
label_widget.after( 10 , open_camera)
button1 = Button(app, text = "Open Camera" , command = open_camera)
button1.pack()
app.mainloop()
|
Output:
Similar Reads
How to open a website in a Tkinter window?
In this article, we are going to see how we can open a website in the Tkinter window. We can open a website in Tkinter using webview. This library allows us to view the HTML content in its GUI window. Syntax to install Tkinter and webview using the below commands. pip install tk pip install pywebvie
2 min read
How to use Thread in Tkinter Python
Prerequisite: Python GUI â tkintermultithreading 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
2 min read
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 tkinter
3 min read
How to play sounds in Python with Tkinter?
Python GUI tkinter is very useful when we want to take data from users. User attracts from GUI. GUI is very helpful in day to day life. A graphical user interface helps us to make our daily tasks easier and very productive. If you want to play music with the help of python GUI tkinter, then you come
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 f
2 min read
How to Close a Tkinter Window With a Button?
Prerequisites: Tkinter Python's Tkinter module offers the Button function to create a button in a Tkinter Window to execute any task once the button is clicked. The task can be assigned in the command parameter of Button() function. Given below are various methods by which this can be achieved. Meth
3 min read