Recoding YouTube with python /u/NoTip5044 Python Education

Hi guys I’m trying to remake youtube out of python, and the buttons of the main window are not appearing. Can someone help me ? Here’s the code : Imports import tkinter as tk from tkinter import messagebox, filedialog import sqlite3 import os import platform import cv2 from PIL import Image, ImageTk

List of videos

videos = []

SQLite Database

conn = sqlite3.connect(“youpy.db”) cursor = conn.cursor()

cursor.execute(“”” CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ) “””)

cursor.execute(“”” CREATE TABLE IF NOT EXISTS videos ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, path TEXT NOT NULL, uploader TEXT NOT NULL ) “””)

conn.commit()

Registration and login functions

Registration window

def register_window(): global reg_win, reg_username_entry, reg_password_entry reg_win = tk.Toplevel(login_window) # The parent window is ‘login_window’ reg_win.title(“Register”) reg_win.geometry(“300×200”)

tk.Label(reg_win, text="Username").pack(pady=5) reg_username_entry = tk.Entry(reg_win) reg_username_entry.pack(pady=5) tk.Label(reg_win, text="Password").pack(pady=5) reg_password_entry = tk.Entry(reg_win, show="*") reg_password_entry.pack(pady=5) tk.Button(reg_win, text="Register", command=register).pack(pady=10) tk.Button(reg_win, text="Cancel", command=reg_win.destroy).pack(pady=5) 

Registration function

def register(): username = reg_username_entry.get() password = reg_password_entry.get() if not username or not password: messagebox.showerror(“Error”, “All fields are required.”) return try: cursor.execute(“INSERT INTO users (username, password) VALUES (?, ?)”, (username, password)) conn.commit() messagebox.showinfo(“Success”, “Registration successful!”) reg_win.destroy() # Closes the registration window except sqlite3.IntegrityError: messagebox.showerror(“Error”, “Username already taken.”)

Login function

def login(): username = login_username_entry.get() password = login_password_entry.get() cursor.execute(“SELECT * FROM users WHERE username = ? AND password = ?”, (username, password)) if cursor.fetchone(): messagebox.showinfo(“Welcome”, f”Welcome, {username}!”) login_window.withdraw() # Hides the login window main_app(username) # Opens the main application window else: messagebox.showerror(“Error”, “Invalid credentials.”)

Video-related functions

def upload_video(username): upload_window = tk.Toplevel() upload_window.title(“Upload Video”) upload_window.geometry(“400×200”)

tk.Label(upload_window, text="Enter video title:").pack(pady=5) title_entry = tk.Entry(upload_window, width=40) title_entry.pack(pady=5) def select_file(): filepath = filedialog.askopenfilename( title="Select a video", filetypes=(("MP4 File", "*.mp4"), ("All Files", "*.*")) ) if filepath and os.path.exists(filepath): # Check if file exists title = title_entry.get().strip() if not title: messagebox.showerror("Error", "A title is required for the video.") return try: cursor.execute("INSERT INTO videos (title, path, uploader) VALUES (?, ?, ?)", (title, filepath, username)) conn.commit() messagebox.showinfo("Success", f"Video '{title}' uploaded successfully.") upload_window.destroy() display_videos_on_homepage() # Refresh the homepage except Exception as e: messagebox.showerror("Error", f"Unable to save the video: {str(e)}") else: messagebox.showerror("Error", "File not found or selection canceled.") 

Function to play videos

def play_video(video): video_window = tk.Toplevel() video_window.title(video[“title”]) video_window.geometry(“800×600”)

# Display video thumbnail cap = cv2.VideoCapture(video["path"]) ret, frame = cap.read() # Read the first frame cap.release() if ret: img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((400, 300)) # Resize thumbnail img_tk = ImageTk.PhotoImage(img) video_label = tk.Label(video_window, image=img_tk) video_label.image = img_tk # Keep a reference video_label.pack(pady=20) # Add an event to play the video when the thumbnail is clicked video_label.bind("<Button-1>", lambda e: play_with_default_player(video["path"])) # Button to play video with external player tk.Button(video_window, text="Play Video", command=lambda: play_with_default_player(video["path"]), bg="green", fg="white").pack(pady=20) # Button to close the window tk.Button(video_window, text="Close", command=video_window.destroy, bg="red", fg="white").pack(pady=10) 

def play_with_default_player(video_path): try: if platform.system() == “Windows”: os.startfile(video_path) # Open video with default player on Windows elif platform.system() == “Darwin”: # macOS os.system(f”open {video_path}”) else: # Linux os.system(f”xdg-open {video_path}”) except Exception as e: messagebox.showerror(“Error”, f”Unable to play video: {str(e)}”)

Display uploaded videos

def display_videos_on_homepage(): for widget in main_window.winfo_children(): widget.destroy()

videos_frame = tk.Frame(main_window) videos_frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True) cursor.execute("SELECT title, path, uploader FROM videos") videos = cursor.fetchall() if not videos: return for video in videos: title, path, uploader = video cap = cv2.VideoCapture(path) ret, frame = cap.read() cap.release() if not ret: continue img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((150, 150)) img_tk = ImageTk.PhotoImage(img) video_button = tk.Button( videos_frame, image=img_tk, text=title, compound="top", command=lambda p=path, t=title, u=uploader: play_video({"title": t, "path": p, "uploader": u}) ) video_button.image = img_tk video_button.pack(pady=10, padx=10, side=tk.LEFT, anchor="n") 

Main application function

def main_app(username): global main_window, animated_label

main_window = tk.Toplevel() main_window.title("Youpy") main_window.geometry("1200x800") main_window.configure(bg="white") top_frame = tk.Frame(main_window, bg="lightgray", height=50) top_frame.pack(side=tk.TOP, fill=tk.X) logout_button = tk.Button( top_frame, text="Logout", command=lambda: [main_window.destroy(), login_window.deiconify()], bg="red", fg="white" ) logout_button.pack(side=tk.RIGHT, padx=10, pady=5) upload_button = tk.Button( top_frame, text="Upload Video", command=lambda: upload_video(username), bg="green", fg="white" ) upload_button.pack(side=tk.LEFT, padx=10, pady=5) user_button = tk.Button( top_frame, text=username, command=lambda: your_channel(username), bg="blue", fg="white" ) user_button.pack(side=tk.LEFT, padx=10, pady=5) animated_label = tk.Label(main_window, text="", font=("Arial", 24), fg="blue", bg="white") animated_label.pack(pady=20) display_videos_on_homepage() 

Login interface

login_window = tk.Tk() login_window.title(“Login”) tk.Label(login_window, text=”Login”, font=(“Arial”, 16)).pack(pady=10) tk.Label(login_window, text=”Username”).pack() login_username_entry = tk.Entry(login_window) login_username_entry.pack() tk.Label(login_window, text=”Password”).pack() login_password_entry = tk.Entry(login_window, show=”*”) login_password_entry.pack() tk.Button(login_window, text=”Login”, command=login).pack(pady=10) tk.Button(login_window, text=”Register”, command=lambda: [login_window.withdraw(), register_window()]).pack() login_window.mainloop()

submitted by /u/NoTip5044
[link] [comments]

​r/learnpython Hi guys I’m trying to remake youtube out of python, and the buttons of the main window are not appearing. Can someone help me ? Here’s the code : Imports import tkinter as tk from tkinter import messagebox, filedialog import sqlite3 import os import platform import cv2 from PIL import Image, ImageTk List of videos videos = [] SQLite Database conn = sqlite3.connect(“youpy.db”) cursor = conn.cursor() cursor.execute(“”” CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ) “””) cursor.execute(“”” CREATE TABLE IF NOT EXISTS videos ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, path TEXT NOT NULL, uploader TEXT NOT NULL ) “””) conn.commit() Registration and login functions Registration window def register_window(): global reg_win, reg_username_entry, reg_password_entry reg_win = tk.Toplevel(login_window) # The parent window is ‘login_window’ reg_win.title(“Register”) reg_win.geometry(“300×200″) tk.Label(reg_win, text=”Username”).pack(pady=5) reg_username_entry = tk.Entry(reg_win) reg_username_entry.pack(pady=5) tk.Label(reg_win, text=”Password”).pack(pady=5) reg_password_entry = tk.Entry(reg_win, show=”*”) reg_password_entry.pack(pady=5) tk.Button(reg_win, text=”Register”, command=register).pack(pady=10) tk.Button(reg_win, text=”Cancel”, command=reg_win.destroy).pack(pady=5) Registration function def register(): username = reg_username_entry.get() password = reg_password_entry.get() if not username or not password: messagebox.showerror(“Error”, “All fields are required.”) return try: cursor.execute(“INSERT INTO users (username, password) VALUES (?, ?)”, (username, password)) conn.commit() messagebox.showinfo(“Success”, “Registration successful!”) reg_win.destroy() # Closes the registration window except sqlite3.IntegrityError: messagebox.showerror(“Error”, “Username already taken.”) Login function def login(): username = login_username_entry.get() password = login_password_entry.get() cursor.execute(“SELECT * FROM users WHERE username = ? AND password = ?”, (username, password)) if cursor.fetchone(): messagebox.showinfo(“Welcome”, f”Welcome, {username}!”) login_window.withdraw() # Hides the login window main_app(username) # Opens the main application window else: messagebox.showerror(“Error”, “Invalid credentials.”) Video-related functions def upload_video(username): upload_window = tk.Toplevel() upload_window.title(“Upload Video”) upload_window.geometry(“400×200″) tk.Label(upload_window, text=”Enter video title:”).pack(pady=5) title_entry = tk.Entry(upload_window, width=40) title_entry.pack(pady=5) def select_file(): filepath = filedialog.askopenfilename( title=”Select a video”, filetypes=((“MP4 File”, “*.mp4”), (“All Files”, “*.*”)) ) if filepath and os.path.exists(filepath): # Check if file exists title = title_entry.get().strip() if not title: messagebox.showerror(“Error”, “A title is required for the video.”) return try: cursor.execute(“INSERT INTO videos (title, path, uploader) VALUES (?, ?, ?)”, (title, filepath, username)) conn.commit() messagebox.showinfo(“Success”, f”Video ‘{title}’ uploaded successfully.”) upload_window.destroy() display_videos_on_homepage() # Refresh the homepage except Exception as e: messagebox.showerror(“Error”, f”Unable to save the video: {str(e)}”) else: messagebox.showerror(“Error”, “File not found or selection canceled.”) Function to play videos def play_video(video): video_window = tk.Toplevel() video_window.title(video[“title”]) video_window.geometry(“800×600”) # Display video thumbnail cap = cv2.VideoCapture(video[“path”]) ret, frame = cap.read() # Read the first frame cap.release() if ret: img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((400, 300)) # Resize thumbnail img_tk = ImageTk.PhotoImage(img) video_label = tk.Label(video_window, image=img_tk) video_label.image = img_tk # Keep a reference video_label.pack(pady=20) # Add an event to play the video when the thumbnail is clicked video_label.bind(“<Button-1>”, lambda e: play_with_default_player(video[“path”])) # Button to play video with external player tk.Button(video_window, text=”Play Video”, command=lambda: play_with_default_player(video[“path”]), bg=”green”, fg=”white”).pack(pady=20) # Button to close the window tk.Button(video_window, text=”Close”, command=video_window.destroy, bg=”red”, fg=”white”).pack(pady=10) def play_with_default_player(video_path): try: if platform.system() == “Windows”: os.startfile(video_path) # Open video with default player on Windows elif platform.system() == “Darwin”: # macOS os.system(f”open {video_path}”) else: # Linux os.system(f”xdg-open {video_path}”) except Exception as e: messagebox.showerror(“Error”, f”Unable to play video: {str(e)}”) Display uploaded videos def display_videos_on_homepage(): for widget in main_window.winfo_children(): widget.destroy() videos_frame = tk.Frame(main_window) videos_frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True) cursor.execute(“SELECT title, path, uploader FROM videos”) videos = cursor.fetchall() if not videos: return for video in videos: title, path, uploader = video cap = cv2.VideoCapture(path) ret, frame = cap.read() cap.release() if not ret: continue img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((150, 150)) img_tk = ImageTk.PhotoImage(img) video_button = tk.Button( videos_frame, image=img_tk, text=title, compound=”top”, command=lambda p=path, t=title, u=uploader: play_video({“title”: t, “path”: p, “uploader”: u}) ) video_button.image = img_tk video_button.pack(pady=10, padx=10, side=tk.LEFT, anchor=”n”) Main application function def main_app(username): global main_window, animated_label main_window = tk.Toplevel() main_window.title(“Youpy”) main_window.geometry(“1200×800″) main_window.configure(bg=”white”) top_frame = tk.Frame(main_window, bg=”lightgray”, height=50) top_frame.pack(side=tk.TOP, fill=tk.X) logout_button = tk.Button( top_frame, text=”Logout”, command=lambda: [main_window.destroy(), login_window.deiconify()], bg=”red”, fg=”white” ) logout_button.pack(side=tk.RIGHT, padx=10, pady=5) upload_button = tk.Button( top_frame, text=”Upload Video”, command=lambda: upload_video(username), bg=”green”, fg=”white” ) upload_button.pack(side=tk.LEFT, padx=10, pady=5) user_button = tk.Button( top_frame, text=username, command=lambda: your_channel(username), bg=”blue”, fg=”white” ) user_button.pack(side=tk.LEFT, padx=10, pady=5) animated_label = tk.Label(main_window, text=””, font=(“Arial”, 24), fg=”blue”, bg=”white”) animated_label.pack(pady=20) display_videos_on_homepage() Login interface login_window = tk.Tk() login_window.title(“Login”) tk.Label(login_window, text=”Login”, font=(“Arial”, 16)).pack(pady=10) tk.Label(login_window, text=”Username”).pack() login_username_entry = tk.Entry(login_window) login_username_entry.pack() tk.Label(login_window, text=”Password”).pack() login_password_entry = tk.Entry(login_window, show=”*”) login_password_entry.pack() tk.Button(login_window, text=”Login”, command=login).pack(pady=10) tk.Button(login_window, text=”Register”, command=lambda: [login_window.withdraw(), register_window()]).pack() login_window.mainloop() submitted by /u/NoTip5044 [link] [comments] 

Hi guys I’m trying to remake youtube out of python, and the buttons of the main window are not appearing. Can someone help me ? Here’s the code : Imports import tkinter as tk from tkinter import messagebox, filedialog import sqlite3 import os import platform import cv2 from PIL import Image, ImageTk

List of videos

videos = []

SQLite Database

conn = sqlite3.connect(“youpy.db”) cursor = conn.cursor()

cursor.execute(“”” CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ) “””)

cursor.execute(“”” CREATE TABLE IF NOT EXISTS videos ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, path TEXT NOT NULL, uploader TEXT NOT NULL ) “””)

conn.commit()

Registration and login functions

Registration window

def register_window(): global reg_win, reg_username_entry, reg_password_entry reg_win = tk.Toplevel(login_window) # The parent window is ‘login_window’ reg_win.title(“Register”) reg_win.geometry(“300×200”)

tk.Label(reg_win, text="Username").pack(pady=5) reg_username_entry = tk.Entry(reg_win) reg_username_entry.pack(pady=5) tk.Label(reg_win, text="Password").pack(pady=5) reg_password_entry = tk.Entry(reg_win, show="*") reg_password_entry.pack(pady=5) tk.Button(reg_win, text="Register", command=register).pack(pady=10) tk.Button(reg_win, text="Cancel", command=reg_win.destroy).pack(pady=5) 

Registration function

def register(): username = reg_username_entry.get() password = reg_password_entry.get() if not username or not password: messagebox.showerror(“Error”, “All fields are required.”) return try: cursor.execute(“INSERT INTO users (username, password) VALUES (?, ?)”, (username, password)) conn.commit() messagebox.showinfo(“Success”, “Registration successful!”) reg_win.destroy() # Closes the registration window except sqlite3.IntegrityError: messagebox.showerror(“Error”, “Username already taken.”)

Login function

def login(): username = login_username_entry.get() password = login_password_entry.get() cursor.execute(“SELECT * FROM users WHERE username = ? AND password = ?”, (username, password)) if cursor.fetchone(): messagebox.showinfo(“Welcome”, f”Welcome, {username}!”) login_window.withdraw() # Hides the login window main_app(username) # Opens the main application window else: messagebox.showerror(“Error”, “Invalid credentials.”)

Video-related functions

def upload_video(username): upload_window = tk.Toplevel() upload_window.title(“Upload Video”) upload_window.geometry(“400×200”)

tk.Label(upload_window, text="Enter video title:").pack(pady=5) title_entry = tk.Entry(upload_window, width=40) title_entry.pack(pady=5) def select_file(): filepath = filedialog.askopenfilename( title="Select a video", filetypes=(("MP4 File", "*.mp4"), ("All Files", "*.*")) ) if filepath and os.path.exists(filepath): # Check if file exists title = title_entry.get().strip() if not title: messagebox.showerror("Error", "A title is required for the video.") return try: cursor.execute("INSERT INTO videos (title, path, uploader) VALUES (?, ?, ?)", (title, filepath, username)) conn.commit() messagebox.showinfo("Success", f"Video '{title}' uploaded successfully.") upload_window.destroy() display_videos_on_homepage() # Refresh the homepage except Exception as e: messagebox.showerror("Error", f"Unable to save the video: {str(e)}") else: messagebox.showerror("Error", "File not found or selection canceled.") 

Function to play videos

def play_video(video): video_window = tk.Toplevel() video_window.title(video[“title”]) video_window.geometry(“800×600”)

# Display video thumbnail cap = cv2.VideoCapture(video["path"]) ret, frame = cap.read() # Read the first frame cap.release() if ret: img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((400, 300)) # Resize thumbnail img_tk = ImageTk.PhotoImage(img) video_label = tk.Label(video_window, image=img_tk) video_label.image = img_tk # Keep a reference video_label.pack(pady=20) # Add an event to play the video when the thumbnail is clicked video_label.bind("<Button-1>", lambda e: play_with_default_player(video["path"])) # Button to play video with external player tk.Button(video_window, text="Play Video", command=lambda: play_with_default_player(video["path"]), bg="green", fg="white").pack(pady=20) # Button to close the window tk.Button(video_window, text="Close", command=video_window.destroy, bg="red", fg="white").pack(pady=10) 

def play_with_default_player(video_path): try: if platform.system() == “Windows”: os.startfile(video_path) # Open video with default player on Windows elif platform.system() == “Darwin”: # macOS os.system(f”open {video_path}”) else: # Linux os.system(f”xdg-open {video_path}”) except Exception as e: messagebox.showerror(“Error”, f”Unable to play video: {str(e)}”)

Display uploaded videos

def display_videos_on_homepage(): for widget in main_window.winfo_children(): widget.destroy()

videos_frame = tk.Frame(main_window) videos_frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True) cursor.execute("SELECT title, path, uploader FROM videos") videos = cursor.fetchall() if not videos: return for video in videos: title, path, uploader = video cap = cv2.VideoCapture(path) ret, frame = cap.read() cap.release() if not ret: continue img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img.thumbnail((150, 150)) img_tk = ImageTk.PhotoImage(img) video_button = tk.Button( videos_frame, image=img_tk, text=title, compound="top", command=lambda p=path, t=title, u=uploader: play_video({"title": t, "path": p, "uploader": u}) ) video_button.image = img_tk video_button.pack(pady=10, padx=10, side=tk.LEFT, anchor="n") 

Main application function

def main_app(username): global main_window, animated_label

main_window = tk.Toplevel() main_window.title("Youpy") main_window.geometry("1200x800") main_window.configure(bg="white") top_frame = tk.Frame(main_window, bg="lightgray", height=50) top_frame.pack(side=tk.TOP, fill=tk.X) logout_button = tk.Button( top_frame, text="Logout", command=lambda: [main_window.destroy(), login_window.deiconify()], bg="red", fg="white" ) logout_button.pack(side=tk.RIGHT, padx=10, pady=5) upload_button = tk.Button( top_frame, text="Upload Video", command=lambda: upload_video(username), bg="green", fg="white" ) upload_button.pack(side=tk.LEFT, padx=10, pady=5) user_button = tk.Button( top_frame, text=username, command=lambda: your_channel(username), bg="blue", fg="white" ) user_button.pack(side=tk.LEFT, padx=10, pady=5) animated_label = tk.Label(main_window, text="", font=("Arial", 24), fg="blue", bg="white") animated_label.pack(pady=20) display_videos_on_homepage() 

Login interface

login_window = tk.Tk() login_window.title(“Login”) tk.Label(login_window, text=”Login”, font=(“Arial”, 16)).pack(pady=10) tk.Label(login_window, text=”Username”).pack() login_username_entry = tk.Entry(login_window) login_username_entry.pack() tk.Label(login_window, text=”Password”).pack() login_password_entry = tk.Entry(login_window, show=”*”) login_password_entry.pack() tk.Button(login_window, text=”Login”, command=login).pack(pady=10) tk.Button(login_window, text=”Register”, command=lambda: [login_window.withdraw(), register_window()]).pack() login_window.mainloop()

submitted by /u/NoTip5044
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *