STRhythm
STRhythm

Reputation: 113

Clearing objects from a frame, without deleting the Frame

I'm attempting to create a simple point of sale system where when a button is pressed, its quantity and price are added up and eventually a total is shown (haven't gotten to this bit yet)

I've decided to also incorporate a clear button which will clear the frame in which the clicked items and their prices + quantities are shown however I'm having some problems with clearing the frame and still being able to click buttons afterwards.

This is the code I have for the Item button:

def AddButton():
    global item_num #calls global variable
    item_num += 1
    item_text = "Chips        2.00"+"       "+str(item_num) #concatonates text & variable
    item1.config(text=item_text) #updates label text - doesn't add multiple 
    item1.pack()
    
addButton = Button(itemFrame, text="Chips", width=10, height=10, command=AddButton)
addButton.grid(row=1, column=1)
item1 = Label(receiptFrame)

and I began by trying to use .destroy like this:

def clearClick(): #blank function for clear button
    receiptFrame.destroy()

however, since this completely deletes the frame, I'm then not able to re-input more items after it's been cleared

I also tried re-creating the frame:

def clearClick(): #blank function for clear button
    receiptFrame.destroy()
    receiptFrame = Frame(root, width=600, height=500, bd=5, relief="ridge")
    receiptFrame.grid(row=1, column=3, columnspan=2)

but this still doesn't work

Is there a way to clear the contents of a frame without deleting the frame itself or does .destroy have to be used?

Upvotes: 0

Views: 249

Answers (1)

hussic
hussic

Reputation: 1920

fr.winfo_children() returns the list of widgets inside the frame:

root = tk.Tk()
fr = tk.Frame()
lb = tk.Label(fr)
lb.grid()
print(fr.winfo_children())
for child in fr.winfo_children():
    child.destroy()
print(fr.winfo_children()) # Now empty

Upvotes: 1

Related Questions