您好我在使用嵌套循环创建按钮网格时遇到问题,并找到该标识以为每个按钮分配唯一的声音或噪音。到目前为止,我们经历了反复试验,发现我们无法为每个按钮提供唯一的变量或访问它们。这是迄今为止的代码:
from Tkinter import *
import random
root = Tk()
root.geometry("1000x600")
list=['kick','snare','pit bull','hi-hat']
colors=['blue', 'red', 'pink','royal blue','green','orange','cyan','purple','magenta','hot pink', 'gold','yellow','aqua','chartreuse', 'slate blue', 'blue violet', 'steel blue', 'dodger blue', 'lime', 'teal', 'plum', 'orchid','crimson', 'dark orange']
b=[]
frame=Frame(root)
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=N+S+E+W)
grid=Frame(frame)
grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
Grid.rowconfigure(frame, 0, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
for x in range(10):
for y in range(4):
btn = (Button(frame,text=list[y] + " " +str(x+1), bg="white", activebackground=random.choice(colors)))
btn.bind("<Enter>", lambda event, b=btn: b.configure(bg=random.choice(colors)))
btn.bind("<Leave>", lambda event, b=btn: b.configure(bg="white"))
btn.bind("<Button-1>", lambda event, b=btn: b.configure(bg=random.choice(colors)))
btn.bind("<Button-3>", lambda event, b=btn: b.configure(bg=random.choice(colors)))
btn.grid(column=x, row=y, sticky=N+S+E+W)
root.mainloop()