What I need, is a menu in tkinter, where each item has the name of a playlist, and each item will go to the same function (ImportPlaylist()
) , but passes a different variable, namely: the rownumber for a csv file where that playlist is stored.
This cannot be done just by hand, because it happens dynamically, i.e. the user can make his own set of playlist with his own names.
I have made a music player, and am just experimenting now with reading and writing csv files. I made a csv file, and made a function to save playlists to that csv file. The way they are saved is that in each row, the first item is the name of the playlist, after that come youtube links one by one.
What I want is now to have functions for importing the playlists. The functions should be called from a tkinter menu. So they need to be created at the opening of the program. I was hoping to do this with a for-loop. So, the for loop would go through the csv file row by row, and for every row create a menu-item, whose name is the first item in the row, and who refers to a function ImportPlaylist, passing some argument to indicate from which row the playlist needs to be imported. Is this even possible?
To import a playlist, I made this little bit of code, generating menu items in a tkinter window:
with open(os.path.join(sys.path[0], "AntiTubePlaylists.txt"), "r") as csv_file:
csv_reader = csv.reader(csv_file, quotechar='"', delimiter=',')
counter = 0
for row in csv_reader:
playlistmenu.add_command(label=row[0], command=(lambda: ImportPlaylist(counter)))
counter += 1
This doesn't work. Which I kind of understand. I suppose counter
is a global variable, and therefor all my nicely generated menu items refer to the same number, which is an empty row.
Any thoughts on this?