I am creating a program which generates a random list of songs, and has a function such that if a user wants to save a song from the random generated list, the user should click the button next to it. Then the user can print the songs he/she saved on a new window, but the function for that in my code seems to only print the saved songs in the terminal and I only get empty window. How can I implement this correctly?
This the code:
from tkinter import *
from PIL import ImageTk, Image
import random
root = Tk()
root.title('UMIND')
root.geometry('')
a = [
'Bruised and Scarred - Mayday Parade',
'All Too Well - Taylor Swift',
'Gravity - Sara Bareilles',
'Perfectly Perfect - Simple Plan',
'Welcome To The Black Parade - My Chemical Romance',
'Everything Has Changed - Taylor Swift',
'Champagne - Taylor Swift',
'Piece of Your Heart - Mayday Parade',
'Blame It On The Rain - He Is We',
'Sad Song - We The Kings',
'Give It All - He Is We',
'Heavy - Linkin Park',
'Ride - Twenty One Pilot',
'One more light - Linkin Park',
'Ride Home - Ben and Ben',
'Leaves - Ben and Ben',
'Fall - Ben and Ben',
'Maybe the night - Ben and Ben',
'Sunrise - Ben and Ben'
]
def show():
global alist
alist = []
if genre.get() == 'Favorite':
# alist.clear()
top = Toplevel()
top.title('Your Playlist')
for i, title in enumerate(random.sample(a, k=10)):
myLabel = Label(top, text=title, font='times 12', anchor=W)
myLabel.grid(column=2, columnspan=2, sticky=W + E, row=i)
Button(top,
text=str(i + 1) + ".",
border=5,
padx=5,
pady=5,
command=lambda title=title: alist.append(title)
).grid(column=0, row=i)
btn2 = Button(top, text='close window', command=top.destroy).grid(row=12, column=1, columnspan=3, sticky=W + E)
def my_playlist():
global alist
blist = []
global myLabel
for song in alist:
if song not in alist:
blist.append(song)
top = Toplevel()
top.title('Your Playlist')
for i, title in enumerate(blist):
myLabel = Label(top, text=title, font='times 12', anchor=W)
myLabel.grid(column=2, columnspan=2, sticky=W + E, row=i)
print(alist)
options = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Favorite"
]
genre = StringVar()
genre.set('From what genre?')
drop = OptionMenu(root, genre, *options)
drop.grid(row=2, column=0, ipadx=20, ipady=10, padx=15, pady=15)
generate_btn = Button(root, text="Generate Playlist", command=show).grid(row=2, column=1, ipadx=30, ipady=10, padx=15,
pady=15)
playlist_btn = Button(root, text="My Playlist", command=my_playlist).grid(row=3, column=0, ipadx=30, ipady=10,
padx=15, pady=15)
Grid.columnconfigure(root, 0, weight=1)
row_con = [ drop, generate_btn, playlist_btn]
row_num = 0
for row in row_con:
Grid.rowconfigure(root, row_num, weight=1)
row_num += 1
root.mainloop()