简体   繁体   English

如何让 tkinter 按钮自行销毁?

[英]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 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.我正在寻找的解决方案可能是完全破坏按钮(首选)或在全屏 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.如您所见,我想让按钮 1 自行销毁。

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 . PS:请阅读本文

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:编辑:如果您收到b1None的错误,请尝试:

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您需要在底部添加b1.place()选项才能使其正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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