Disabling buttons after click in Tkinter

Viewed 26355

I'm new to Python and I'm trying to make a simple application using Tkinter.

def appear(x):
    return lambda: results.insert(END, x)

letters=["A", "T", "D", "M", "E", "A", "S", "R", "M"] 

for index in range(9): 
    n=letters[index] 
    nButton = Button(buttons, bg="White", text=n, width=5, height=1,
    command =appear(n), relief=GROOVE).grid(padx=2, pady=2, row=index%3,
    column=index/3)

What I'm trying to do is disable the buttons once I click them. I tried

def appear(x):
    nButton.config(state="disabled")
    return lambda: results.insert(END, x)

But it gives me the following error:

NameError: global name 'nButton' is not defined

3 Answers

If you want to disable a button after clicking you can make a function in which you will do the desired operation but in the start you should a command to disable the button so that as the function runs it desables the button . if you want you can take a little bit help with this code:-

from tkinter import *
root = Tk()

def disable_button():
    button_1['state'] = DISABLED
    print("this is how you disable a buuton after a click")
button_1 = Button(root,text = "Disable this button",command=disable_button)
button_1.pack()
root.mainloop()

I hope your problem has been solved

Thanks

Related