Python Tkinter clearing a frame

后端 未结 3 1283
醉话见心
醉话见心 2020-12-23 19:52

I am trying to clear out a frame in the tkinter so that the new contents can be written (refresh information) but i could not manage to do it. I am aware of these



        
3条回答
  •  庸人自扰
    2020-12-23 20:38

    For clear frame, first need to destroy all widgets inside the frame,. it will clear frame.

    import tkinter as tk
    from tkinter import *
    root = tk.Tk()
    
    frame = Frame(root)
    frame.pack(side="top", expand=True, fill="both")
    
    lab = Label(frame, text="hiiii")
    lab.grid(row=0, column=0, padx=10, pady=5)
    
    def clearFrame():
        # destroy all widgets from frame
        for widget in frame.winfo_children():
           widget.destroy()
        
        # this will clear frame and frame will be empty
        # if you want to hide the empty panel then
        frame.pack_forget()
    
    frame.but = Button(frame, text="clear frame", command=clearFrame)
    frame.but.grid(row=0, column=1, padx=10, pady=5)
    
    # then whenever you add data in frame then you can show that frame
    lab2 = Label(frame, text="hiiii")
    lab2.grid(row=1, column=0, padx=10, pady=5)
    frame.pack()
    root.mainloop()
    

提交回复
热议问题