我正在创建一个生成随机歌曲列表的程序,并且我想执行一个函数,如果用户想从随机生成的列表中保存一首歌曲,用户应该单击它旁边的按钮。似乎该函数仅获取列表中的最后一首歌曲/元素。我怎样才能正确地实现这一点?我将如何防止在 alist 中有类似或重复的歌曲?
这是代码:
from tkinter import *
import random
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',
'Leave - Ben and Ben',
'Fall - Ben and Ben',
'Maybe the night - Ben and Ben',
'Sunrise - Ben and Ben'
]
root = Tk()
root.title('GUI test')
root.iconbitmap('icon2.ico')
alist=[]
def list_generator():
for title in random.sample(a, k=10):
myLabel = Label(frame, text=title, font='times 12', anchor=W)
myLabel.grid(column=2, columnspan=2, sticky=W+E)
for i in range(10):
Button(frame,
text=str(i + 1) + ".",
border=5,
padx=5,
pady=5,
command=lambda: alist.append(title)
).grid(column=0, row=i)
def list():
print(alist)
frame = LabelFrame(root, border=3, relief=GROOVE)
frame.grid(row=1, column=0, padx=30, pady=20)
myButton = Button(root, text="Generate Playlist?", padx=87, pady=20, command=list_generator,
fg="white", bg="#c0c0c0", font=("Bold", 16))
myButton.grid(row=0, column=0, columnspan=3)
exit_btn = Button(root, text='Exit Program', command=root.quit)
exit_btn.grid(row=3, column=0)
sl = Button(root, text='Your list', command=list)
sl.grid(row=2, column=0)
root.mainloop()