How to start videos from a tkinter window

A simple app to start videos using tkinter to click on the mp4 to be launched.

import tkinter as tk
import os, glob


root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
for f in os.listdir():
    if f.endswuith(".mp4"):
        lb.insert(0, f)

files = glob.glob("*.mp4")

root.mainloop()
This app just shows the files in the folder

Now to start the videos we need to bind the double click to the window.

def launch_mp4(event):
	cur = lb.curselection()
	get_filename = lb.get(cur)
	os.startfile(get_filename)

lb.bind("", launch_mp4)

This will do the work.

To launch a video from youtube

When you hit enter the browser will open and the video will start
def launch_youtube(event):
	url = entry.get()
	os.system(f"start {url}")

lab = tk.Label(root, text="Enter youtube url").pack()
entry = tk.Entry(root)
entry.pack()
entry.bind("", launch_youtube)

The whole code

import tkinter as tk
import os, glob


root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
[lb.insert(0, f) for f in os.listdir() if f.endswith(".mp4")]

files = glob.glob("*.mp4")

def launch_mp4(event):
	cur = lb.curselection()
	get_filename = lb.get(cur)
	os.startfile(get_filename)



lb.bind("", launch_mp4)

def launch_youtube(event):
	url = entry.get()
	os.system(f"start {url}")

lab = tk.Label(root, text="Enter youtube url").pack()
entry = tk.Entry(root)
entry.pack()
entry.bind("", launch_youtube)


root.mainloop()

github repo

Capture the video link with clipboard

If you do not want to copy and paste the url address adding this function and the button to activate the function

	root1 = tk.Tk()
	clip = root1.clipboard_get()
	root1.withdraw()
	e.set(clip)
	print(clip)
	root1.destroy()

The whole code

import tkinter as tk
import os, glob


root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
[lb.insert(0, f) for f in os.listdir() if f.endswith(".mp4")]

files = glob.glob("*.mp4")

def launch_mp4(event):
	cur = lb.curselection()
	get_filename = lb.get(cur)
	os.startfile(get_filename)

def get_clipboard():
	root1 = tk.Tk()
	clip = root1.clipboard_get()
	root1.withdraw()
	e.set(clip)
	print(clip)
	root1.destroy()

lb.bind("<Double-Button>", launch_mp4)

def launch_youtube(event):
	url = entry.get()
	os.system(f"start {url}")


lab = tk.Label(root, text="Enter youtube url").pack()
e = tk.StringVar()
entry = tk.Entry(root, textvariable=e)
entry.pack()
entry.bind("<Return>", launch_youtube)

b_cnf = {
	"text" : "Get clipboard",
	"command": get_clipboard
}

b = tk.Button(cnf=b_cnf)
b.pack()

b2_cnf = {
	"text" : "Launch",
	"command": lambda : launch_youtube("event")
}

b2 = tk.Button(cnf=b2_cnf)
b2.pack()
root.mainloop()
After you copied the url, you can just press get clipboard to copy it in the entry box and then press launch


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.