Hiding a progressbar in tkinter

Cecil Westerhof Cecil at decebal.nl
Fri Jun 28 06:04:17 EDT 2019


MRAB <python at mrabarnett.plus.com> writes:

> On 2019-06-26 16:47, Cecil Westerhof wrote:
>> I just started with GUI stuff in tkinter. I have a progressbar, but I
>> want it to be only visible when it is used. So I tried the following:
>>      window = Tk()
>>      window.title(window_str)
>>      frame  = Frame(window)
>>      frame.pack(side = "top", fill = "both", expand = True)
>>      Button(window, text = button_str, command = select_dir).pack()
>>      progress = ttk.Progressbar(window, orient = "horizontal", length = 200,
>>                                 mode = "determinate")
>>      progress.pack()
>>      progress.lower(frame)
>>      window.mainloop()
>>
>> But that does not hide the progressbar. What am I doing wrong?
>>
>> I could use pack_forget, but that will change the dimensions of the
>> window.
>>
> The progress bar isn't hidden because there's nothing on top of it to
> hide it.
>
> import tkinter as tk
> import tkinter.ttk as ttk
>
> window = tk.Tk()
> window.title(window_str)
> frame = tk.Frame(window)
> frame.pack(side='top', fill='both', expand=True)
> tk.Button(window, text=button_str, command=select_dir).pack()
>
> # Create a frame to hold the progress bar.
> progress_frame = tk.Frame(window)
> progress_frame.pack(fill='x', expand=True)
>
> # Put the progress bar into the progress frame, ensuring that it fills
> the grid cell.
> progress = ttk.Progressbar(progress_frame, orient='horizontal',
> length=200, mode='determinate')
> progress.grid(row=0, column=0, sticky='nsew')
>
> # Now put a blank frame into the progress frame over the progress bar,
> ensuring that it fills the same grid cell.
> blank = tk.Frame(progress_frame)
> blank.grid(row=0, column=0, sticky='nsew')
>
> # Raise the progress bar over the blank frame to reveal it.
> progress.tkraise()
>
> # Lower the progress bar underneath the blank frame to hide it.
> progress.lower()
>
> window.mainloop()

Works like a charm. Thanks.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list