Jon Bailey
Jon Bailey

Reputation: 103

Tkinter in Python - Remove widget from active window

I'm trying to remove a Tkinter progress bar widget from an active window (after the GUI window using Tkinter has been initialized). I'm using a Tkinter Frame for my window. I've initialized the progress bar as pb, as below.

pb = ttk.Progressbar(root,orient ="horizontal",length = 540, mode ="determinate")

And then I've tried two different methods to get rid of the progress bar. The line below causes the window to freeze and stop responding when I try to use it after the GUI is initialized.

pb.pack_forget()

The line below causes only a middle section of the progress bar to disappear, but you can still see the two sides of it.

pb.destroy()

Is there any way I could get this widget to disappear after the Frame has been initialized?

Upvotes: 4

Views: 8720

Answers (2)

Ozzius
Ozzius

Reputation: 139

Sorry for my bad English. This code worked for me. I simply follow Oakley's instruction.

def progressBar(*args, **kwargs):

def progress(currentValue):
    progressbar["value"] = currentValue

maxValue = 100

progressbar = ttk.Progressbar((kwargs), orient="horizontal", length=150, mode="determinate", takefocus=True)
progressbar.pack(side=tk.BOTTOM)

currentValue = 0
progressbar["value"] = currentValue
progressbar["maximum"] = maxValue

divisions = 10
for i in range(divisions):
    currentValue = currentValue + 10
    progressbar.after(500, progress(currentValue))
    progressbar.update()  # Force an update of the GUI

progressbar.destroy()

Hence I simply tried progressbar.destroy() outside of the loader loop. So after complete the loading, it will disappear from the main App window.

Thank you, Bryan Oakley sir.

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385890

The specific answer to your question is that pack_forget, grid_forget or grid_remove are what you want if you want to make a widget temporarily invisible. Which one you choose depends on if you're using grid or pack, and whether or not you want grid to remember where it was so you can later put it back in the same spot.

destroy is what you want to call if you want to literally destroy the widget.

When used properly, none of those methods will cause your program to freeze. Without seeing your code, it's impossible to know what the root cause of the problem is.

Upvotes: 6

Related Questions