Python 更改tkinter中按钮的颜色

Python 更改tkinter中按钮的颜色,python,button,tkinter,Python,Button,Tkinter,在尝试制作电梯模拟器时,我遇到了一个问题。 共有4部电梯,当按下楼层编号时,它应变为红色。这适用于1个电梯中的1个“面板”,但不适用于每个电梯中的1个“面板”。 这是我的密码: def floorChooserButtons( self, eleNum, floors, yStart, yEnd, xStart, xEnd): self.Buttons1 = [i for i in range(41)] self.Buttons2 = [i for i in range(41)]

在尝试制作电梯模拟器时,我遇到了一个问题。 共有4部电梯,当按下楼层编号时,它应变为红色。这适用于1个电梯中的1个“面板”,但不适用于每个电梯中的1个“面板”。 这是我的密码:

def floorChooserButtons( self, eleNum, floors, yStart, yEnd, xStart, xEnd):
    self.Buttons1 = [i for i in range(41)]
    self.Buttons2 = [i for i in range(41)]
    self.Buttons3 = [i for i in range(41)]
    self.Buttons4 = [i for i in range(41)]
    if(eleNum is 1):
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons1[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons1[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 2):
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons2[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons2[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 3):
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons3[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons3[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 4):
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons4[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons4[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
        self.QUIT = tk.Button(self, text="QUIT", fg="red",
            command=root.destroy).grid(row = xPos, column = yPos)


def chooser(self, index, eleNum):
    print("Number", index, "pressed in elevator", eleNum)
    if eleNum is 1:
        self.Buttons1[index].configure(bg="red")
    if eleNum is 2:
        self.Buttons2[index].configure(bg="red")
    if eleNum is 3:
        self.Buttons3[index].configure(bg="red")
    if eleNum is 4:
        self.Buttons4[index].configure(bg="red")
eleNum是电梯的编号,它是1-4 yStart、yEnd、xStart、xEnd都用于布局

任何帮助都是巨大的。
谢谢

您可以通过多种方式更改按钮的颜色

b = Button(background = 'red')
b.config(background = 'red')
b.configure(background = 'red')
b['background'] = 'red'

b = Button(bg = 'red')
b.config(bg = 'red')
b.configure(bg = 'red')
b['bg'] = 'red'

您可以使用
前台
fg

更改字体颜色。这是误入的代码。@hd1这只是由于堆栈溢出,我已尝试将其缩进,但它不允许我缩进。它在我的代码中是正确缩进的。@MartynRushton:你说它不允许你是什么意思?当然会的。将所有选项卡替换为空格,插入代码,高亮显示,然后单击
{}
按钮。如果你希望人们努力回答你的问题,你应该努力提问。好吧,结果我搞砸了……现在就完成了=D