Hi,
I am making a music player in python 2.6 with Tkinter. Here's my code :

from Tkinter import *
import mp3play
import tkFileDialog
import Tkinter
def open_file():                                #Opens a dialog box to open .mp3 file
global music                                #then sends filename to file_name_label.
global mp3
global play_list
filename.set (tkFileDialog.askopenfilename(defaultextension = ".mp3", filetypes=[("All Types", ".*"), ("MP3", ".mp3")]))
playlist = filename.get()
playlist_pieces = playlist.split("/")
play_list.set (playlist_pieces[-1])
playl = play_list.get()
play_list_display.insert(END, playl)
mp3 = filename.get()
print mp3
music = mp3play.load(mp3)
pieces = mp3.split("/")
name.set (pieces[-1])

def play():                                     #Plays the .mp3 file
music.play()

def stop():                                     #Stops the .mp3 file
music.stop()                
def pause():                                    #Pauses or unpauses the .mp3 file
if music.ispaused() == True:
    music.unpause()
elif music.ispaused() == False:
    music.pause()

def vol(event):                                 #Allows volume to be changed with the slider
v = Scale.get(volume_slider)
music.volume(v)
def Exit():
exit()
root = Tk()
root.title("EmoPlayer")
root.geometry('300x100+250+100')
filename = Tkinter.StringVar()
name = Tkinter.StringVar()
play_list = Tkinter.StringVar()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label='File', menu = filemenu)
filemenu.add_command(label='Open', command = open_file)
filemenu.add_separator()
filemenu.add_command(label='Exit', command = Exit)
root.config(menu=menubar)
open_file = Button(root, width = 6, height = 1, text = 'Open file', command = open_file)
open_file.grid(row=0, column=3)
play_button = Button(root, width = 5, height = 1, text='Play', command = play)
play_button.grid(row=0, column=0, sticky = W)
stop_button = Button(root, width = 5, height = 1, text='Stop', command = stop)
stop_button.grid(row=0, column=1, sticky = W)
pause_button = Button(root, width = 5, height = 1, text='Pause', command = pause)
pause_button.grid(row=0, column=2)
volume_slider = Scale(root, label='Volume', orient = 'horizontal', fg = 'black', command = vol)
volume_slider.grid(row=0, column=4)
file_name_label = Label(root, font=('Verdana', 8), fg = 'black', wraplength = 300, textvariable=name )
file_name_label.grid(row=3, column=0, columnspan=8)
play_list_window = Toplevel(root, height = 150, width = 100)
play_list_window.title("Playlist")
play_list_display = Listbox(play_list_window, width = 50)   
play_list_display.pack()

play_list_window.mainloop()
root.mainloop()

This code can open a mp3 file, insert it in playlist and play it. But it cant allow to play song by selecting it from playlist. Can anyone tell me how to make things work by selecting from playlist?

Dani AI

Generated

For : the missing piece is a stable mapping from the Listbox entry to the full file path, plus a selection handler that loads and plays that path. Right now you insert only the base filename into the Listbox, so when an item is clicked there’s no stored path to load. Also avoid shadowing functions with widget names (e.g. don’t assign a Button to the same name as a function) and don’t call a Toplevel’s own mainloop — call root.mainloop() once. was right about fixing indentation and the misuse of globals outside functions.

A minimal pattern to follow (keep a parallel list of full paths, stop any current track, load the selected path, then play):

import os

playlist_paths = []            # full paths in the same order as listbox
# when adding a file:
def add_track(fullpath):
    playlist_paths.append(fullpath)
    playlist_box.insert(END, os.path.basename(fullpath))

# play the selected item (bind to double-click and/or Play button)
def play_selection(event=None):
    sel = playlist_box.curselection()
    if not sel:
        return
    idx = int(sel[0])
    path = playlist_paths[idx]
    global music
    try:
        music.stop()
    except NameError:
        pass
    music = mp3play.load(path)
    music.play()

playlist_box.bind('<Double-Button-1>', play_selection)

Extra tips: declare global music only inside functions that modify it, use clearer widget names (open_button, play_button), handle <<ListboxSelect>> if you want to update a label on single-click, and consider keeping state in a small class instead of many globals. If mp3play gives compatibility problems on newer Python builds, try alternatives (pygame.mixer or pydub).

First of all you must indent your code correctlly. And global is meaningless outside of functions.

Also put your function definitions right after import statements. play, stop and exit functions are compeletely unnecessary, however. as they only call another function.

Do use single empty lines (double after imports) to clarify structure of your code, with discretion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.