-7

I want in my if statement for it to destroy the buttons on my tkinter. I have tried a couple of methods and looked up a few and some i don't understand/too complicated. I have tried making the function create a new window but it isn't displaying.

def greenwin():
    global tkinter
    global Tk
    root = Tk()
    root.title("GAME OVER")
    root.geometry('387x387')
    gamelabel=Label(root,text="GAME OVER!GREENS
WIN!",width=33,height=15).place(x=150,y=150)
    root.mainloop
    return

I want a clear method of destroying widgets.I would like a function that destroys all these buttons for my tic tac toe.

but1=Button(root,text="",bg="white",width=11,height=5,command=colour1).place(x=0,y=0)
but2=Button(root,text="",bg="white",width=11,height=5,command=colour2).place(x=0,y=150)
but3=Button(root,text="",bg="white",width=11,height=5,command=colour3).place(x=0,y=300)
but4=Button(root,text="",bg="white",width=11,height=5,command=colour4).place(x=150,y=0)
but5=Button(root,text="",bg="white",width=11,height=5,command=colour5).place(x=150,y=150)
but6=Button(root,text="",bg="white",width=11,height=5,command=colour6).place(x=150,y=300)
but7=Button(root,text="",bg="white",width=11,height=5,command=colour7).place(x=300,y=0)
but8=Button(root,text="",bg="white",width=11,height=5,command=colour8).place(x=300,y=150)
but9=Button(root,text="",bg="white",width=11,height=5,command=colour9).place(x=300,y=300)
root.mainloop
Nae
  • 14,209
  • 7
  • 52
  • 79
  • 3
    Isn't it literally `widget.destroy()`? – Nae Jan 25 '18 at 18:07
  • @Piinthesky I'll upload my code now – gabe Levey Jan 25 '18 at 18:10
  • @Nae no it doesn't work – gabe Levey Jan 25 '18 at 18:11
  • 2
    This sounds like an XY problem. Usually when we get this question it turns out that the *real* question is "how do I change the displayed frame?", which has a completely different answer. Please expand your question to include the big picture of what the user should experience and include a [mcve]. – Novel Jan 25 '18 at 18:12
  • @gabeLevey Sure it may not, neither the way you're asking the question though. We can keep on guessing what your code looks like, or you could prepare a [mcve] and get a determined and fast answer. – Nae Jan 25 '18 at 18:15
  • Try using **`{}`** button after pasting, and selecting your code to format it better. – Nae Jan 25 '18 at 18:16

2 Answers2

1
import tkinter as tk


root = tk.Tk()
any_widget = tk.Button(root, text="Press to destroy!")
any_widget['command'] = any_widget.destroy  # pay special attention to the lack of ()
# call any_widget.destroy(), button widget's command option specifically needs a
# reference to the method instead of an actual call
any_widget.pack()
root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
0

Try this:

import tkinter as tk

root = tk.Tk()
root.geometry("500x300+10+13")
root.title("Test")

b = tk.Button(root, text="click me")

def onclick(evt):
    w = evt.widget
    w.destroy()

b.bind("<Button-1>", onclick)

b.pack()
root.mainloop()
7stud
  • 46,922
  • 14
  • 101
  • 127
  • 1
    You forgot the `bind` – Novel Jan 25 '18 at 18:09
  • 1
    And the `root.mainloop()` – PM 2Ring Jan 25 '18 at 18:09
  • 1
    What is the "evt" bit @7stud – gabe Levey Jan 25 '18 at 18:13
  • @Novel where would I put the bind? – gabe Levey Jan 25 '18 at 18:16
  • @Novel, Thanks. Copy/paste error cut off the last 3 lines. Fixed. – 7stud Jan 25 '18 at 18:19
  • Okay, voting to close now. – 7stud Jan 25 '18 at 18:22
  • @7stud thanks a lot. Do you know how I could replicate that into my code – gabe Levey Jan 25 '18 at 18:22
  • @gabeLevey, GUI programming, i.e. with tkinter, isn't for beginners. You need some user action to trigger the destruction of the buttons. What user action do you want that to be? – 7stud Jan 25 '18 at 18:25
  • @7stud It would be easier if I could do that without the user triggerring it, would prefer it to be from an if statement. But if that is impossible I could perhaps do it from when the user clicks a button – gabe Levey Jan 25 '18 at 18:33
  • @gabeLevey, But the if-statement has to execute. What is going to trigger execution of the if statement? In GUI programming, all code execution is triggered by some event, e.g. the user clicks on a button, or the user moves the mouse, or the user hits the Return key. Or, are you saying that you want to delete your Buttons before they are even displayed to the user? – 7stud Jan 25 '18 at 18:36
  • @gabeLevey, *What is the "evt" bit* -- when you bind() a function to a button, when the button is clicked python calls the function and passes it an event object as an argument. The event object contains information about what the user did to trigger the event, and one property of the event object is the actual widget upon which the event occurred. – 7stud Jan 25 '18 at 19:04
  • @7stud no, in the tic tac toe I created, I created an algorithm that knows when someone has won. I want once someone wins for a new window to be displayed or for the buttons to be removed – gabe Levey Jan 26 '18 at 16:07
  • @gabeLevey, Well, your algorithm doesn't just execute at random times, right? Your algorithm executes in response to some user event. – 7stud Jan 26 '18 at 17:15