一旦按下按钮,就会隐藏一种禁用按钮的方式?

时间:2013-12-01 01:19:51

标签: python button lambda tkinter

这是按钮,按钮没有名字!

代码:

  Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i:
  self.letterGuess(alpha[j])).grid(row=1, column=self.columnz(i))

我在Tkinter类中创建了一个循环,创建了26个字母按钮。现在,当按下按钮时,会执行回调函数(letterGuess),命令行会打印出“按钮%s%字母“。

但我也想在按下按钮时禁用按钮。怎么可能这样做?

编辑: 这是循环:

    alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    buttonFrame = Frame(self).grid(row=0, column=1)
    for i in range(len(alpha)):

        if i <= 6:
             Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j])).grid(row=1, column=self.columnz(i))
        elif i > 6 and i <=13:
            Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j])).grid(row=2, column=self.columnz(i))
        elif i > 13 and i <=20:
            Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j])).grid(row=3, column=self.columnz(i))
        elif i > 20 and i <26:
            Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j])).grid(row=4, column=self.columnz(i))

1 个答案:

答案 0 :(得分:0)

您需要按钮参考 - 因此您需要将Button指定给变量

temp = Button(...)
temp.config(command=lambda j=i, button=temp: self.letterGuess(alpha[j], button))
temp.grid(...)

现在您可以在letterGuess中引用此按钮,您可以使用它来禁用按钮

def letterGuess(letter, button):
    button.config(state=DISABLED)

但是如果您以后需要更改按钮状态,可以在列表或字典中保存引用

# before loop
all_buttons = dict()

# inside loop
all_buttons[ alpha[i] ] = Button(...)
all_buttons[ alpha[i] ].grid(...) 

# in function
def letterGuess(letter):
    all_buttons[ letter ].config(state=DISABLED)

修改

alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
buttonFrame = Frame(self).grid(row=0, column=1)

all_buttons = dict()

for i in range(len(alpha)):

    if i <= 6:
         all_buttons[ alpha[i] ] = Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j]))
         all_buttons[ alpha[i] ].grid(row=1, column=self.columnz(i))
    elif 6 < i <=13:
         all_buttons[ alpha[i] ] = Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j]))
         all_buttons[ alpha[i] ].grid(row=2, column=self.columnz(i))
    elif 13 < i <=20:
         all_buttons[ alpha[i] ] = Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j]))
         all_buttons[ alpha[i] ].grid(row=3, column=self.columnz(i))
    elif 20 < i <26:
         all_buttons[ alpha[i] ] = Button(buttonFrame, height=2, width=5, text = alpha[i], command=lambda j=i: self.letterGuess(alpha[j]))
         all_buttons[ alpha[i] ].grid(row=4, column=self.columnz(i))

<强>顺便说一句:

您可以使用6 < i <=13代替i > 6 and i <= 13

您可以使用

for i, letter in enumerate(alpha):

然后您可以使用letter代替alpha[i]