In this tutorial, we will create a simple music player using Python’s Tkinter library. Tkinter provides a fast and easy way to create GUI applications. Our music player will allow you to play, pause, and stop audio files, giving you hands-on experience with event handling, file operations, and the pygame
library for audio playback.
Simple music player GUI app using Tkinter
import tkinter as tk from tkinter import filedialog import pygame root = tk.Tk() root.title("Simple Music Player") root.geometry("400x300") pygame.mixer.init() def play_music(): pygame.mixer.music.play() def pause_music(): pygame.mixer.music.pause() def stop_music(): pygame.mixer.music.stop() def load_music(): file_path = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3")]) if file_path: pygame.mixer.music.load(file_path) status_label.config(text="Loaded: " + file_path.split('/')[-1]) play_button = tk.Button(root, text="Play", command=play_music) pause_button = tk.Button(root, text="Pause", command=pause_music) stop_button = tk.Button(root, text="Stop", command=stop_music) load_button = tk.Button(root, text="Load Music", command=load_music) status_label = tk.Label(root, text="No Music Loaded") load_button.pack(pady=10) play_button.pack(pady=10) pause_button.pack(pady=10) stop_button.pack(pady=10) status_label.pack(pady=20) root.mainloop()
Adding Music Player Functionality
- Load Music: Opens a file dialog to load an MP3 file.
- Play: Plays the loaded music.
- Pause: Pauses the currently playing music.
- Stop: Stops the music playback.
Running the Application
Save the music_player.py
file and run it using Python:
python music_player.py