简体   繁体   中英

How do I make a tkinter button destroy itself?

I made this program in Tkinter in python where a small window pops up when the code is run and a start button would pop up and make the window full screen and show the content after. I want to make the button destroy itself after I press it so it makes a fullscreen and removes the button. I am still a beginner and would like the answer to be simple. The solution I am looking for is to maybe destroy the button completely(preferred) or move it way out of sight in the fullscreen window. Here is the code:

import Tkinter as w
from Tkinter import *

w = Tk()

w.geometry("150x50+680+350")
def w1():
    w.attributes("-fullscreen", True)
    l1 = Label(w, text = "Loaded!", height = 6, width = 8).pack()
    global b1
    b1.place(x = -10000, y = -10000)



b1 = Button(w, text = "Start", height = 3, width = 20, command = w1).place(x = 0, y = 10)
b2 = Button(w, text = "Exit", command = w.destroy).place(x = 1506, y = 0)

w.mainloop()

As you can see I want to make button one destroy itself.

Try this:

import tkinter as tk # Use `import Tkinter as tk` for Python 2

root = tk.Tk()
root.geometry("150x50+680+350")

def function():
    global button_start
    root.attributes("-fullscreen", True)
    label = tk.Label(root, text="Loaded!", height=6, width=8)
    label.pack()
    button_start.place_forget() # You can also use `button_start.destroy()`



button_start = tk.Button(root, text="Start", height=3, width=20, command=function)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)

root.mainloop()

PS: Please read this .

Try:

b1.place_forget()

This will essentially "forget" about the button and hide it from view.

Edit: If you are getting the error that b1 is None try:

b1 = Button(w, text = "Start", height = 3, width = 20, command = w1)
b1.place(x = 0, y = 10)

You need to add the b1.place() option at the bottom for this to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM