我之前没有成功回答过这个问题,所以我会再试一次。
下面是(应该)按下时更改按钮背景颜色的代码。它基本上采用数字'eleNum',然后使用它打印出4到40个按钮,范围从0到40.
这是奇怪的一点。面板编号4按预期工作,但其余部分只是给我一个错误。例如,如果我按下面板2中的按钮:
line 113, in chooser
self.Buttons2[index].onfigure(bg="orange")
AttributreError: 'int' object has no attribute 'configure'
这是我的代码。
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)]
self.eleNumber = [i for i in range(4)]
if(eleNum is 1):
self.eleNumber[0] = tk.Label(self, width = 12, text="Elevator 1")
self.eleNumber[0].grid(row = xStart-1, column =yStart+1, columnspan=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.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):
self.eleNumber[1] = tk.Label(self, width = 12, text="Elevator 2")
self.eleNumber[1].grid(row = xStart-1, column =yStart+1, columnspan=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.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):
self.eleNumber[2] = tk.Label(self, width = 12, text="Elevator 3")
self.eleNumber[2].grid(row = xStart-1, column =yStart+1, columnspan=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):
self.eleNumber[3] = tk.Label(self, width = 12, text="Elevator 4")
self.eleNumber[3].grid(row = xStart-1, column =yStart+1, columnspan=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.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="blue")
if eleNum is 2:
self.Buttons2[index].configure(bg="orange")
if eleNum is 3:
self.Buttons3[index].configure(bg="pink")
if eleNum is 4:
self.Buttons4[index].configure(bg="red")
答案 0 :(得分:1)
您的列表中有整数。你自己把它们放在那里,例如:self.Buttons2 = [i for i in range(41)]
。在此之后,您只将其中一些更改为按钮,例如:self.Buttons2[floor] = tk.Button( ...
。
然后尝试在整数上调用configure
方法 - 错误消息直接解释了这一点。显然整数没有configure
方法。
因此,您的代码始终只适用于一个面板。在给定floorChooserButtons
的情况下调用eleNum
后,所有列表都将重置为整数,只有与eleNum
对应的列表正在填充按钮(仍然只对{{1}内的索引填充列表)。
快速解决方法是移动部件:
floors
到构造函数。然后,在为所有面板调用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)]
之后,您将使用实际按钮填充它们,而不是数字。你的整个结构对我来说仍然没有意义(为什么你会在列表中有整数,意图在它们中存储按钮?)。
作为旁注,您错误地使用了floorChooserButtons
运算符。它在意外工作,因为Python中的整数是不可变的,但它不是很可读。对于价值平等测试,您应该使用is
运算符。