Tkinter:按钮和命令

时间:2021-01-08 10:50:52

标签: python tkinter tkinter-button

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

1 个答案:

答案 0 :(得分:2)

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

来自@acw1668 的comment

  • 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()