Python Forum
[Tkinter] the first item in listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] the first item in listbox
#1
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
Reply
#2
listbox.get(0) will return the txt of the first item in the listbox.
Reply
#3
(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)
Reply
#4
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)
rwahdan likes this post
Reply
#5
Thank you very much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get the selected item from Listbox and convert it to int? Jionni 8 5,139 Feb-17-2020, 11:07 AM
Last Post: Jionni
  [Tkinter] taking bad listbox items after lowering it to 1 item deadmarshal 3 3,133 Jul-18-2018, 04:16 PM
Last Post: woooee
  seprating columns of an listbox item and putting them into different entry gray 2 4,244 Feb-26-2017, 12:53 PM
Last Post: gray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020