Home > Net >  Destroy the Tkinter button after it is clicked
Destroy the Tkinter button after it is clicked

Time:12-07

I am trying to make a chance game on Tkinter. I am trying to make the button disappear after it is clicked. This should work for all the buttons - once a button is clicked, it no longer becomes viewable to the user. The buttons are in the last half of my code, from the Start() function.

from tkinter import *
from tkinter import messagebox
import random

point = 0

def showerror():
    messagebox.showerror("Loser","Next time :)")
    messagebox.showinfo("Your point", "Point: "   str(point))

def addpoint():
    global point
    point  =  31

def deleteButton():
    pass

def test():
    r = random.randint(0,7)
    if r > 4:
        showerror()
    elif r <= 4:
        addpoint()

tk = Tk()
tk.title("Are u Lucky")
tk.geometry("400x300")

def Start():
    P1 = Label(tk, text="Choose")
    P1.place(x=157,y=10)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=10 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=50 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=90 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=130 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=170 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=210 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=250 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=290 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=330 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=370 ,y=30)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=10 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=50 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=90 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=130 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=170 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=210 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=250 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=290 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=330 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=370 ,y=60)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=10 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=50 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=90 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=130 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=170 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=210 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=250 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=290 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=330 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=370 ,y=90)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=10 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=50 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=90 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=130 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=170 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=210 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=250 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=290 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=330 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=370 ,y=120)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=10 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=50 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=90 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=130 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=170 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=210 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=250 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=290 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=330 ,y=150)
    P2 = Button(tk, text="  X   ",command=test)
    P2.place(x=370 ,y=150)

tk.mainloop()

CodePudding user response:

This is not perfect, but I had made a few improvements to your code. Some TIPS:

  • Use Loops, Almost always you can make a loop for redundant work ie. copying and pasting 50x buttons
  • I changed how you initialize the window if your code event works
  • Also 'P1' and 'p2' is bad good naming convention but I do the same so idk

To answer your question: You can use .destroy() OR ( .forget_pack() and .forget_grid() ) for any object

Code:

import random
from tkinter import *
from tkinter import messagebox

point = 0
class Main():

    def showerror():
        messagebox.showerror("Loser","Next time :)")
        messagebox.showinfo("Your point", "Point: "   str(point))

    def addpoint():
        global point
        point  =  31

    def deleteButton(btn):
        btn.destroy()

    def test(btn):
        r = random.randint(0,7)
        if r > 4:
            Main.showerror()
        elif r <= 4:
            Main.addpoint()

        Main.deleteButton(btn)

    def __init__(root, master):

        root.master = master
        window = master

        window.title("Are u Lucky")
        window.geometry("400x300")

        P1 = Label(window, text="Choose")

        for x in range(1, 20):
            for y in range(1, 20):
                button = Button(window, text="  X   ",)
                button.place(x=(10 x*40) ,y=(30 y*30))
                button.configure(command=lambda btn=button: Main.test(btn))

root = Tk()
window = Main(root)
root.mainloop()

  • Related