2014-01-30 101 views
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都用於佈局。

任何幫助將被粉碎。 感謝

+0

這是misindented代碼更改字體顏色。 – hd1

+0

@ hd1只是由於stackoverflow,我試圖縮進它,它不允許我。它在我的代碼中正確縮進。 – Swapnull

+0

@MartynRushton:你是什麼意思,它不會允許你?當然會的。用空格替換所有選項卡,插入代碼,突出顯示它,然後單擊'{}'按鈕。如果你希望人們努力回答你的問題,你應該努力去問。 –

回答

2

可以更改按鈕的顏色在許多方面

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