在Tkinter的更改按钮颜色 [英] Changing colour of buttons in tkinter

查看:1505
本文介绍了在Tkinter的更改按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然试图使电梯模拟器我所遇到的一个问题。
有4部电梯和地板时数为pressed它应更改为红色。这适用于1 1电梯'面板',但不适合在每个电梯1'面板'。
这里是我的code:

While trying to make an elevator simulator I have come across a problem. There are 4 elevators and when the floor number is pressed it should change to be red. This works for 1 'panel' in 1 elevator, but not for 1 'panel' in each elevator. Here is my code :

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都用于布局。

eleNum is the number of the elevator, it is 1-4 yStart, yEnd, xStart, xEnd are all used for the layout.

任何帮助将粉碎。
谢谢

Any help would be smashing. Thanks

推荐答案

您可以更改按钮的颜色在很多方面

You can change the color of a button in many ways

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

You can change the font color with foreground or fg

这篇关于在Tkinter的更改按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆