|
| 1 | +import pygame |
| 2 | +import tkinter as tkr |
| 3 | +from tkinter.filedialog import askdirectory |
| 4 | +import os |
| 5 | + |
| 6 | +music_player = tkr.Tk() |
| 7 | +music_player.title("My Music Player") |
| 8 | +music_player.geometry("450x350") |
| 9 | +directory = askdirectory() |
| 10 | +os.chdir(directory) |
| 11 | +song_list = os.listdir() |
| 12 | + |
| 13 | +play_list = tkr.Listbox(music_player, font="Helvetica 12 bold", bg='yellow', selectmode=tkr.SINGLE) |
| 14 | +for item in song_list: |
| 15 | + pos = 0 |
| 16 | + play_list.insert(pos, item) |
| 17 | + pos += 1 |
| 18 | +pygame.init() |
| 19 | +pygame.mixer.init() |
| 20 | + |
| 21 | +def play(): |
| 22 | + pygame.mixer.music.load(play_list.get(tkr.ACTIVE)) |
| 23 | + var.set(play_list.get(tkr.ACTIVE)) |
| 24 | + pygame.mixer.music.play() |
| 25 | +def stop(): |
| 26 | + pygame.mixer.music.stop() |
| 27 | +def pause(): |
| 28 | + pygame.mixer.music.pause() |
| 29 | +def unpause(): |
| 30 | + pygame.mixer.music.unpause() |
| 31 | +Button1 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="PLAY", command=play, bg="blue", fg="white") |
| 32 | +Button2 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="STOP", command=stop, bg="red", fg="white") |
| 33 | +Button3 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="PAUSE", command=pause, bg="purple", fg="white") |
| 34 | +Button4 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="UNPAUSE", command=unpause, bg="orange", fg="white") |
| 35 | + |
| 36 | +var = tkr.StringVar() |
| 37 | +song_title = tkr.Label(music_player, font="Helvetica 12 bold", textvariable=var) |
| 38 | + |
| 39 | +song_title.pack() |
| 40 | +Button1.pack(fill="x") |
| 41 | +Button2.pack(fill="x") |
| 42 | +Button3.pack(fill="x") |
| 43 | +Button4.pack(fill="x") |
| 44 | +play_list.pack(fill="both", expand="yes") |
| 45 | +music_player.mainloop() |
0 commit comments