This code only centers horizontally, how can I make the progress bar also centered vertically ?
import Tkinter
import ttk
root = Tkinter.Tk()
root.geometry("=500x500")
root.progressbar = ttk.Progressbar(root)
root.progressbar.pack(anchor=Tkinter.CENTER)
root.mainloop()
In this specific case, adding expand=True
to the pack statement will suffice.
Don't take this as general purpose advice for how to center any widget in any situation. expand=True
isn't a secret alias for "center this". It just so happens that in this specific case, the combination of expand with the lack of the use of the fill
attribute, the fact that the window is bigger than the progress bar, and the fact there aren't any other widgets, all works together to center this specific widget.
Another way you can center a widget -- especially if it's the only widget in an area -- is to use place
. This is one of the few times where place
might be better than grid
or pack
. place
allows you to explicitly place a widget in the center of its container.
For example:
root.progressbar.place(relx=.5, rely=.5, anchor="c")
This says "place the center of the widget at 50% the width of the container and 50% of the height of the container".