0

我正在创建一个生成随机歌曲列表的程序,并且我想执行一个函数,如果用户想从随机生成的列表中保存一首歌曲,用户应该单击它旁边的按钮。似乎该函数仅获取列表中的最后一首歌曲/元素。我怎样才能正确地实现这一点?我将如何防止在 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()
4

1 回答 1

2
  • 您所在的第二个循环list_generator只能访问最新的title值,因此我删除了第二个循环并将代码移至第一个循环。
  • 我将循环更改为使用enumerate,以便我们可以获得索引(就像您在第二个循环中所做的那样)。
  • 您绑定到按钮的lambda函数没有title存储的值,这导致所有 lambdas 使用相同的值,所以我将它作为唯一的默认值添加到lambda函数中。
  • 您覆盖了内置关键字listno bueno,我将其更改为print_list.

来自@acw1668 的评论

  • alist被调用时没有被清除list_generator
  • myLabel.grid没有它的row论点。

最终代码:

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():
    alist.clear()
    for i, title in enumerate(random.sample(a, k=10)):
        myLabel = Label(frame, text=title, font='times 12', anchor=W)
        myLabel.grid(column=2, columnspan=2, sticky=W+E, row=i)

        Button(frame,
               text=str(i + 1) + ".",
               border=5,
               padx=5,
               pady=5,
               command=lambda title=title: alist.append(title)
               ).grid(column=0, row=i)


def print_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=print_list)
sl.grid(row=2, column=0)

root.mainloop()
于 2021-01-08T10:59:16.330 回答