Removing the selected files from a playlist

Since the Listbox allows for multiple selections, we iterate through all the selected items, removing them from the frontend Listbox widget as well as from the model play_list, as follows (see code 5.03view.py):

def remove_selected_files(self):
try:
selected_indexes = self.list_box.curselection()
for index in reversed(selected_indexes):
self.list_box.delete(index)
self.model.remove_item_from_play_list_at_index(index)
except IndexError:
pass

Note that we reverse the tuple before removing items from the playlist because we want to start removing items from the end, as a removal causes a change in the index of playlist items. If we do not remove items from the end, we may end up removing the wrong items from the list, as its index gets modified in each iteration.

Since we have defined this method here, let's add it as a command callback to the right-click delete menu, as follows:

def on_remove_selected_context_menu_clicked(self):
self.remove_selected_files()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.51.145