问题
So, for playing the song, the code I have is:
def play_song(*args):
idx = song_list.curselection()[0]
song = song_dict[idx][1]
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
Though now I need a forward button, so what my initial plan was that:
def next_song():
next_one = song_list.curselection()
next_one = next_one[0]+1
song = song_list.get(next_one)
That would give me the next song name but I can't figure out how I would play it using Pygame.
回答1:
Looked like you wanted me to take a look here, I think this is simple. Get the current selection and add one to the current selection and clear the current selection, then set the current selection to the new selection then load and play the song. So the function would be like:
def next_song():
try:
idx = song_list.curselection()[0] + 1 # The initial curselection + 1
song_list.selection_clear(0,'end') # Clear the old selection
song_list.selection_set(idx) # Set selection to new item
song_list.activate(idx) # Set the entire focus to new item?
song = song_dict[idx][1] # Get the corresponding song from the dictionary
pygame.mixer.music.load(song) # Load the song
pygame.mixer.music.play(loops=0) # Play the song
except IndexError: # If no item selected
pass # Ignore the error
Example: So if the current selection is 1 then it removes 1 and makes it 2 and sets the current selection to 2 and activates it(the underline by default) and proceeds like this each time button is pressed.
来源:https://stackoverflow.com/questions/65519693/i-am-making-a-mp3-player-and-i-need-a-forward-button-to-it-but-cant-seem-to-fi