Welcome folks today in this blog post we will be displaying the webcam video in tkinter window using the tkinter-webcam
library. This will a GUI Desktop Application built using the tkinter framework. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below library of tkinter to display webcam video. You can install the library using the pip
command as shown below
pip install tkinter-webcam
After installing it you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
"""Example""" import tkinter as tk from tkinter_webcam import webcam window = tk.Tk() # Sets up GUI window.title("Example") # Titles GUI window.geometry("1000x1000") # Sizes GUI # Uses Box class from webcam to create video window video = webcam.Box(window, width=450, height=450) video.show_frames() # Show the created Box tk.mainloop() |
Code Explanation
As you can see in the first line of code we are importing the tkinter webcam package and also we are importing the base tkinter library as well.
1 2 |
import tkinter as tk from tkinter_webcam import webcam |
And then we are initializing a new tkinter window of width and height using the geometry method as shown below
1 2 3 |
window = tk.Tk() # Sets up GUI window.title("Example") # Titles GUI window.geometry("1000x1000") # Sizes GUI |
And then we are using the webcam.Box() method to display the user webcam video using the built in webcam of the user device. And inside this function we are passing the tkinter window as the first argument and then we are passing the width and height of the webcam video as the second and third argument.
And then guys we are showing the webcam video frames inside the tkinter window
1 2 3 |
# Uses Box class from webcam to create video window video = webcam.Box(window, width=450, height=450) video.show_frames() # Show the created Box |
As you can see we are using the Box class of the webcam module to create the actual webcam video inside the tkinter window. And then we are showing it using the show_frames() method.
And now lastly we are running the tkinter app by calling the mainloop() method which is present inside the base tkinter library as shown below
1 |
tk.mainloop() |
Now if you run the app.py
file by executing the below command as shown below
python app.py