Devon M
Devon M

Reputation: 761

Make button disappear after running procedure

I have a Tkinter window that at the moment has 18 buttons, and they all have the same code:

    Button2=Button(master,text='click me',command=lambda:callback())
    Button2.grid(row=1,column=2)

when I execute this code it runs the procedure

callback 

but I want it to run the procedure and then disappear; I have tried

def hide_me(event):
event.widget.grid_forget()

Button2=Button(master,text='click me',command=lambda:callback())
Button2.bind('<Button-1>',hide_me) 
Button2.grid(row=1,column=2)

but when I press the button it makes the button disappear without executing the callback, and when I try:

def callback(Buttons):

   C = Characters.pop(0)
   Buttons.bind('<Button-1>',hide_me())
   return C()


Button2=Button(master,text='click me',command=lambda:callback(Button2))
Button2.bind('<Button-1>',hide_me) 
Button2.grid(row=1,column=2)

it runs the callback, but the button doesn't disappear. Can anybody tell me what I am doing wrong?

Upvotes: 1

Views: 7548

Answers (1)

sloth
sloth

Reputation: 101142

You should not call bind if you already use the command keyword arg.

Just wrap callback() and grid_forget() into one method:

def callback_and_hide(button):
    callback()
    button.grid_forget()

Button2 = Button(master,text='click me',command=lambda: callback_and_hide(Button2))

Upvotes: 2

Related Questions