Python 我想把几个按钮声明为一个

Python 我想把几个按钮声明为一个,python,tkinter,Python,Tkinter,我想做一个从b0到b10的声明,而不是b0输入if。 /如何在不使用或的情况下将多个按钮更改为一个声明 b0 = tk.Button(root, text = (out0), bg=random.choice(color), command=lambda:code(out0)) b1 = tk.Button(root, text = (out1), bg=random.choice(color), command=lambda:code(out1)) b2 = tk.Button(root, t

我想做一个从b0到b10的声明,而不是b0输入if。 /如何在不使用或的情况下将多个按钮更改为一个声明

b0 = tk.Button(root, text = (out0), bg=random.choice(color), command=lambda:code(out0))
b1 = tk.Button(root, text = (out1), bg=random.choice(color), command=lambda:code(out1))
b2 = tk.Button(root, text = (out2), bg=random.choice(color), command=lambda:code(out2))
b3 = tk.Button(root, text = (out3), bg=random.choice(color), command=lambda:code(out3))
b4 = tk.Button(root, text = (out4), bg=random.choice(color), command=lambda:code(out4))
b5 = tk.Button(root, text = (out5), bg=random.choice(color), command=lambda:code(out5))
b6 = tk.Button(root, text = (out6), bg=random.choice(color), command=lambda:code(out6))
b7 = tk.Button(root, text = (out7), bg=random.choice(color), command=lambda:code(out7))
b8 = tk.Button(root, text = (out8), bg=random.choice(color), command=lambda:code(out8))
b10 = tk.Button(root, text = (out9), bg=random.choice(color), command=lambda:code(out9)

if b0['bg'] == 'red':
      pin = pin[:-1]
请参阅python中的类! 类按钮SELF: 这是关于按钮的代码
这真的很有帮助,因为您可以放置比self更多的参数,然后调用buttonarguments来创建一个新按钮

您可以这样做:

buttons = []
for i in range(11):
    buttons.append( tk.Button(root, text = ('out'+str(i)), bg=random.choice(color), command=lambda:code(i))
if b[0]['bg'] == 'red':
      pin = pin[:-1]

现在按钮是一个列表,按钮i位于索引i处。例如,您只需重写命令codeout1,就可以将i作为输入而不是out1。

您为什么一遍又一遍地问同样的问题?发生了什么事??