In this lesson we want to learn How to Build Music Player with Python TKinter, Building a simple media player in Python using Tkinter can be achieved by using the ttk.Combobox and ttk.Button classes to create a drop-down list for selecting the media files and buttons for controlling the playback. You can use the built-in pygame library to play the media files.
This is an example of how you can build a simple media player in Tkinter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# Importing necessary libraries import tkinter as tk from tkinter import ttk import pygame # Defining a class for the media player class MediaPlayer: def __init__(self, root): # Initializing the root window self.root = root self.root.title("Codeloop - Media Player") # Setting the title of the window self.root.geometry("250x150") # Setting the size of the window # Initializing the playlist with audio files self.playlist = ["audio1.mp3", "audio2.mp3", "audio3.mp3"] self.current_track = tk.StringVar() # Variable to track the current selected track self.current_track.set(self.playlist[0]) # Setting the default track # Creating a Combobox to select tracks from the playlist self.combobox = ttk.Combobox(self.root, values=self.playlist, textvariable=self.current_track) self.combobox.pack() # Creating buttons for controlling playback self.play_button = ttk.Button(self.root, text="Play", command=self.play_track) self.play_button.pack() self.pause_button = ttk.Button(self.root, text="Pause", command=self.pause_track) self.pause_button.pack() self.stop_button = ttk.Button(self.root, text="Stop", command=self.stop_track) self.stop_button.pack() # Initializing the pygame library for playing audio pygame.init() pygame.mixer.init() # Method to play the selected track def play_track(self): pygame.mixer.music.load(self.current_track.get()) # Loading the selected track pygame.mixer.music.play() # Playing the track # Method to pause the currently playing track def pause_track(self): pygame.mixer.music.pause() # Pausing the track # Method to stop the currently playing track def stop_track(self): pygame.mixer.music.stop() # Stopping the track # Main section of the code if __name__ == "__main__": root = tk.Tk() # Creating the root window player = MediaPlayer(root) # Creating an instance of the media player root.mainloop() # Running the Tkinter event loop |
In this example, we have created a MediaPlayer class that takes the root Tkinter window as a parameter. After that we creates a drop-down list ttk.Combobox which is populated with a list of audio files and a set of buttons ttk.Button for controlling the playback. The play_track, pause_track, and stop_track methods are used to control the playback of the media files using the pygame library. You can also add more functionalities like next,previos, volume control, etc. make sure that you have installed pygame. This is just a basic example, you can customize it as according to your requirement.
Run the complete code and this will be the result
FAQs:
How to make a music player in Python?
You can make a music player in Python using libraries such as Tkinter for the GUI and Pygame for audio playback. This is a example and steps:
- Setup GUI: Use Tkinter to create the graphical interface for your music player. You can create buttons for play, pause, stop and select tracks from a playlist.
- Manage Audio: Use Pygame to handle audio playback. Load audio files, play, pause and stop them as needed. Pygame provides functionality for controlling audio streams easily.
- Integrate: Connect the GUI controls with the audio playback functions. For example, when the user clicks the play button, load and play the selected track. Similarly, implement functionality for pausing and stopping playback.
- User Interaction: Ensure smooth interaction by handling user input. Update the GUI interface based on the playback state and allow users to select tracks from the playlist.
Subscribe and Get Free Video Courses & Articles in your Email