Make your First app with Python
When you are just starting to write code, the amount of dopamine boost you get from printing a simple “Hello World” in the terminal is just super awesome, but you need to step up your game so lets make a really really simple yet cool GUI with python.
In this project we’ll make a simple youtube video downloader using Tkinter and the pytube library.
For this we would need to get the libraries.
- Pytube — pip install pytube
- Tkinter — it already comes with python
that’s it!
- let’s import the things we need to, in order to get started!
import tkinter as tk #importing tkinter as tk
import tkinter.ttk as ttk
from pytube import YouTube
global yt #making it global so all the functions can use it
2. Now, let’s start building the UI.
root = tk.Tk()
root.title('YouTube Downloader')
root.geometry("450x650")
root.iconbitmap("D:\\Python_Projects\\Pytube\\youtube_logo_icon_147199.ico")
url_label = tk.Label(root, text="Enter URL")
url_label.grid(row=0, column=0, padx=2)url_entry = tk.Entry(root, width=50)
url_entry.grid(row=0, column=1, pady=20, padx=5)btn_show_size = tk.Button(root, text="Show Size", command=download_17_3gp_size)
btn_show_size.grid(row=3, column=3)
3. Now that’s done it’s time to add some functionality
def download_18_mp4_360p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(18)
size = (stream.filesize / 1048576)
final_size = str(round(size, 2)) + " "+"Mbs"
label = tk.Label(root, text=final_size, font=("Ubuntu", 20))
label.grid(row=8, column=1, pady=0)
btn_show_size = tk.Button(root, text="Show Size", command=download_18_mp4_360p)
btn_show_size.grid(row=4, column=3)
make a function so we could download videos in different quality, as above we’re making a function to download the video in 3GP quality.
4. Similarly we can make different functions for different qualities.
def download_22_mp4_720p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(22)
size = (stream.filesize / 1048576)
final_size = str(round(size, 2)) + " "+"Mbs"
label = tk.Label(root, text=final_size, font=("Ubuntu", 20))
label.grid(row=8, column=1, pady=0)
btn_show_size = tk.Button(root, text="Show Size", command=download_22_mp4_720p)
btn_show_size.grid(row=5, column=3)
def download_137_mp4_1080p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(137)
size = (stream.filesize / 1048576)
final_size = str(round(size, 2)) + " "+"Mbs"
label = tk.Label(root, text=final_size, font=("Ubuntu", 20))
label.grid(row=8, column=1, pady=0)
btn_show_size = tk.Button(root, text="Show Size",
command=download_137_mp4_1080p)
btn_show_size.grid(row=6, column=3)# The quality for the video
# lets define the functions
def download_17_3gp():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(17)
# we created a new directory in D drive
stream.download(output_path="D:\\Downloads")
dwnld_confirm = tk.Label(root, text="Downloaded",
font=("Ubuntu", 20), fg="red")
dwnld_confirm.grid(row=3, column=2)
def download_18_mp4_360p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(18)
# we created a new directory in D drive
stream.download(output_path="D:\\Downloads")
dwnld_confirm = tk.Label(root, text="Downloaded",
font=("Ubuntu", 20), fg="red")
dwnld_confirm.grid(row=4, column=2)
def download_22_mp4_720p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(22)
# we created a new directory in D drive
stream.download(output_path="D:\\Downloads")
dwnld_confirm = tk.Label(root, text="Downloaded",
font=("Ubuntu", 20), fg="red")
dwnld_confirm.grid(row=5, column=2)
def download_137_mp4_1080p():
yt = YouTube(url_entry.get())
stream = yt.streams.get_by_itag(137)
# we created a new directory in D drive
stream.download(output_path="D:\\Downloads")
dwnld_confirm = tk.Label(root, text="Downloaded",
font=("Ubuntu", 20), fg="red")
dwnld_confirm.grid(row=6, column=2)
5. Finally let’s make the download buttons.
btn_17 = tk.Button(root, text='Download 3gp', bg='black',
fg='white', command=download_17_3gp)
btn_17.grid(row=3, column=1, pady=5)
btn_18 = tk.Button(root, text='Download Mp4 360p', bg='black',
fg='white', command=download_18_mp4_360p)
btn_18.grid(row=4, column=1, pady=5)
btn_22 = tk.Button(root, text='Download Mp4 720p', bg='black',
fg='white', command=download_22_mp4_720p)
btn_22.grid(row=5, column=1, pady=5)btn_137 = tk.Button(root, text='Download Mp4 1080p', bg='black',
fg='white', command=download_137_mp4_1080p)
btn_137.grid(row=6, column=1, pady=5)btn_quit = tk.Button(root, text='Quit App!', bg='red', fg='white', font=(
"Ubuntu", 20), command=lambda: root.destroy())
btn_quit.grid(row=7, column=1, pady=150, ipadx=20, ipady=20)
6. Keep it Running until we press the quit button
# keep it running
root.mainloop()
mainloop keeps the code running until we terminate the application.
I hope you like the post! :)