我怎样才能使一个GUI窗口清空它的内容?

时间:2016-02-01 00:14:07

标签: python eclipse tkinter pydev

我正在从事一个学校项目,我对编程非常陌生。我想知道如何使用我制作的GUI,其中包含一组条目/标签/按钮,并使窗口"刷新"就像在普通浏览器中一样。我的目标是在此过程中清除所有条目和按钮的窗口"刷新"。

我提前感谢任何建议,并希望我的帖子足够具体。

这是一个没有任何功能的登录屏幕。

import tkinter
import distutils
from distutils.cmd import Command
import os
from _ast import Delete

window = tkinter. Tk()
window.title("PTF Pydev Eclipse")
window.geometry("500x500")

lbl = tkinter.Label(window, text= "Username")
ent = tkinter.Entry(window)
lbl1 = tkinter.Label(window, text= "Password")
ent1 = tkinter.Entry(window)
lbl2 = tkinter.Label(window, text= "Access")

ent1.bind('<Return>', show) # Not sure if this is needed
btn = tkinter.Button(window, text="Login", command= lambda: validation())

lbl.pack()
ent.pack()
lbl1.pack()
ent1.pack()
lbl2.pack()
btn.pack()

window.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用pack_forget()删除小部件。

lbl.pack_forget()
ent.pack_forget()
lbl1.pack_forget()
ent1.pack_forget()
lbl2.pack_forget()
btn.pack_forget()

您还可以将所有小部件放在Frame()中,然后使用pack_forget()删除内部所有小部件的Frame

import tkinter as tk

def validation():
    # remove frame
    frame.pack_forget()

window = tk.Tk()
window.title("PTF Pydev Eclipse")
window.geometry("500x500")

# create frame
frame = tk.Frame(window)
frame.pack()

# put widgets in frame 
lbl = tk.Label(frame, text= "Username")
ent = tk.Entry(frame)
lbl1 = tk.Label(frame, text= "Password")
ent1 = tk.Entry(frame)
lbl2 = tk.Label(frame, text= "Access")

#ent1.bind('<Return>', show) # Not sure if this is needed
btn = tk.Button(frame, text="Login", command=validation)

ent.pack()
lbl1.pack()
ent1.pack()
lbl2.pack()
btn.pack()

window.mainloop()

BTW:它很有用:"famous" Bryan Oakley's program on how to switch between frames.