0

How to delete the button in tkinter if it got click then play the function example this is the code

from tkinter import *
s = Tk()
#example this the game
def game():
    pass


#if this button got click this button should be gone and play the game
b = Button(s,text="play",command=game)


b.grid(row=1)

s.mainloop()

2 Answers2

0

This is the method your looking for

b.destroy()
XenO
  • 17
  • 3
  • 1
    While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Feb 26 '22 at 09:26
  • 1
    It is just a single line of code and its pretty self explanatory as it just destroys the element – XenO Feb 27 '22 at 17:38
  • 1
    It's not self explanatory because you haven't described or shown how to use and also call `game()`. – martineau Feb 27 '22 at 17:51
0

You can call the grid_remove() method as part of what is executed when the Button is clicked.

from tkinter import *

s = Tk()

def game():
    print('game running')

b = Button(s, text="play",
           command=lambda: (b.grid_remove(), game()))  # Hide button and call game.
b.grid(row=1)

s.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301