Python Forum

Full Version: the first item in listbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I need to know how to get the value of the first item in the listbox?
song_box.select_set(0)
I think this will only make the selection of the first item. I need to play music of playlist. after reaching the end of the list I need to move the path to the first item so it can replay from start again.
last_value = song_box.get("end")
if song == last_value and converted_current_time == converted_song_length:
    thefirst = song_box.select_set(0)
    pygame.mixer.music.load(thefirst)
    pygame.mixer.music.play(loops=0)
elif song != last_value and converted_current_time == converted_song_length:
    next_song()
the second statement is working since i am not reaching the end of the playlist it will go to the next item in the list.

def next_song():
    global paused
    if paused==True:
        paused = False
        play()
    #get current file in the list
    next_one = song_box.curselection()
    # get next file in the list
    next_one = next_one[0] + 1
    #assign the next file in the list
    song = song_box.get(next_one)
    #load and play the file
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    # clear active bar in play list
    song_box.select_clear(0, END)
    # activate bar in play list
    song_box.activate(next_one)
    # set the active bar to next file
    song_box.selection_set(next_one,last=None)
Thanks
listbox.get(0) will return the txt of the first item in the listbox.
(Jun-27-2021, 02:07 PM)Yoriz Wrote: [ -> ]listbox.get(0) will return the txt of the first item in the listbox.

I am getting bad listbox index error:
Error:
self.tk.call(self._w, 'activate', index) tkinter.TclError: bad listbox index "C:/Users/ramit/PycharmProjects/mp3_player/audio/10_الكوثر.mp3": must be active, anchor, end, @x,y, or a number
here is my code again

last_value = song_box.get("end")

if song == last_value and converted_current_time == converted_song_length:
    thefirst = song_box.get(0)
    pygame.mixer.music.load(thefirst)
    pygame.mixer.music.play(loops=0)
    song_box.select_clear(0, END)
    song_box.activate(thefirst)
    song_box.selection_set(thefirst, last=None)

elif song != last_value and converted_current_time == converted_song_length:
    next_song()
So how it is not active?? I have this:
song_box.activate(thefirst)
You don't activate by passing the string value returned by song_box.get(0),
Use the index to activate the first item song_box.activate(0) also song_box.selection_set(0)
Thank you very much