0
尝试制作电梯模拟器时,遇到了问题。 有4部电梯,当按下楼层号码时,它应该变成红色。这适用于1部电梯中的1'面板',但不适用于每部电梯中的1'面板'。 这里是我的代码:更改tkinter中按钮的颜色
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都用于布局。
任何帮助将被粉碎。 感谢
这是misindented代码更改字体颜色。 – hd1
@ hd1只是由于stackoverflow,我试图缩进它,它不允许我。它在我的代码中正确缩进。 – Swapnull
@MartynRushton:你是什么意思,它不会允许你?当然会的。用空格替换所有选项卡,插入代码,突出显示它,然后单击'{}'按钮。如果你希望人们努力回答你的问题,你应该努力去问。 –