Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
0
Indexable
Never
self.import tkinter as tk
import pygame
import os

class MusicPlayer:
    def __init__(self, master):
        self.master = master
        master.title("Music Player")
        master.geometry("400x200")

        # initialize pygame mixer
        pygame.mixer.init()

        # create song list box
        self.song_listbox = tk.Listbox(master, width=50)
        self.song_listbox.pack(pady=10)

        # add songs to list box
        for song in os.listdir("songs"):
            if song.endswith(".mp3"):
                self.song_listbox.insert(tk.END, song)

        # create play button
        self.play_button = tk.Button(master, text="Play", command=self.play_song)
        self.play_button.pack()

        # create stop button
        self.stop_button = tk.Button(master, text="Stop", command=self.stop_song)
        self.stop_button.pack()

    def play_song(self):
        # get selected song from list box
        song = self.song_listbox.get(tk.ACTIVE)

        # load and play song using pygame mixer
        pygame.mixer.music.load(os.path.join("songs", song))
        pygame.mixer.music.play()

    def stop_song(self):
        # stop song using pygame mixer
        pygame.mixer.music.stop()

# create root window
root = tk.Tk()

# create music player
music_player = MusicPlayer(root)

# start main loop
root.mainloop()