0

Many widgets in my program are placed like so:

widget.place(relx=0.5, rely=0.5, anchor=CENTER)

So, I wanted to make this the default setup. I have tried:

root.option_add("*anchor", "center")#Putting CENTER returns an error
root.option_add("*relx", "0.5")
root.option_add("*rely", "0")

However, this does not work.

How could I achieve this?

Anonymous Coder
  • 512
  • 1
  • 6
  • 22
  • I think you _can_ [override the method](https://stackoverflow.com/q/10829200/7032856) `place` for your convenience. – Nae Feb 13 '18 at 14:37

2 Answers2

2

You can't change the default options without overriding the method, but a viable workaround would be simply defining a variable for that customization and simply pass that to the widgets that use it:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


if __name__ == '__main__':
    root = tk.Tk()
    customization = dict(relx=0.5, rely=0.5, anchor='center')
    my_list_of_widgets = list()
    for i in range(30):
        my_list_of_widgets.append(tk.Label(root, text=i))
        my_list_of_widgets[i].place(**customization)
    tk.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
0

You can't. The option database is for specifying widget attributes, not arguments to functions. There is no built-in mechanism for specifying the arguments to pack, place, grid, or any other tkinter functions.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685