Adding and removing items from a playlist

Let's write some code for a feature that allows us to add and remove items from a playlist. To be more specific, we will write the code for a function for the four buttons marked in the following screenshot:

The four buttons, from left to right, perform the following functions:

  • The first button from the left adds individual audio files to a playlist
  • The second button deletes all the selected items from the playlist
  • The third button scans a directory for audio files and adds all the found audio files to the playlist
  • The last button empties the playlist

Since adding these features requires us to interact with the Listbox widget of Tkinter, let's spend some time getting to know the Listbox widget:

We can create a Listbox widget like we'd create any other widget, as follows:

 play_list = tk.ListBox(parent, **configurable options)

When you initially create a Listbox widget, it is empty. To insert one or more lines of text into the Listbox, use the insert() method, which takes two arguments, namely an index of the position where the text needs to be inserted and the actual string that needs to be inserted, as follows:

 play_list.insert(0, "First Item")
play_list.insert(1, "Second Item")
play_list.insert(END, "Last Item")

The curselection() method returns the index of all the items selected in the list, and the get() method returns the list item for a given index, as follows:

 play_list.curselection() # returns a tuple of all selected items
play_list.curselection()[0] # returns first selected item
play_list.get(1) # returns second item from the list
play_list.get(0, END) # returns all items from the list

In addition to this, the Listbox widget has several other configurable options.

For a complete Listbox widget reference, type the following into the Python interactive shell:

 >>> import tkinter
>>> help(tkinter.Listbox)

Now that we know how to add and remove items from the Listbox widget, let's code these functions into the player.

Let's begin by modifying the command callback attached to the four buttons, as follows (see code 5.03view.py):

def on_add_file_button_clicked(self):
self.add_audio_file()

def on_remove_selected_button_clicked(self):
self.remove_selected_file()

def on_add_directory_button_clicked(self):
self.add_all_audio_files_from_directory()

def on_clear_play_list_button_clicked(self):
self.clear_play_list()

def on_remove_selected_context_menu_clicked(self):
self.remove_selected_file()

All that these four methods do is call four other methods that do the actual task of adding or removing items to the playlist. All the methods will update the play_list items at the following two places:

  • In the visible Listbox widget
  • In the backend data structure playlist maintained by the Model class

Let's define the four new methods.

..................Content has been hidden....................

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