我用python的Tkinter编写了这个程序,当代码运行时,会弹出一个小窗口,并弹出一个开始按钮,使窗口全屏显示内容。我想让按钮在我按下后自动销毁,这样它就会全屏显示并移除按钮。我还是个初学者,希望答案简单明了。我正在寻找的解决方案是可能完全销毁按钮(首选)或将其移至全屏窗口中看不见的地方。代码如下:
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()
如你所见,我想让按钮1自行销毁。
发布于 2021-03-14 20:25:00
试试这个:
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:请阅读this。
发布于 2021-03-14 20:33:00
尝试:
b1.place_forget()
这将从本质上“忘记”按钮,并将其隐藏在视图中。
编辑:如果您收到b1
is None
的错误,请尝试:
b1 = Button(w, text = "Start", height = 3, width = 20, command = w1)
b1.place(x = 0, y = 10)
您需要在底部添加b1.place()
选项才能正常工作
https://stackoverflow.com/questions/66629079
复制相似问题