wannik wannik - 15 days ago 4
Python Question

How to set padding of all widgets inside a window or frame in Tkinter

I create a window with two buttons and set the padding of both of them.

from tkinter import *

window = Tk()
Button(window, text="Click Me").pack(padx=10, pady=10)
Button(window, text="Click Me").pack(padx=10, pady=10)
window.mainloop()


I want to remove
padx
and
pady
parameter from
pack()
and getting the same result. How can I do this?

Answer

You can't. Though, you can put the values in variable so that if you want to change the value later, you only have to change it in one place.

padx = 10
pady = 10
Button(...).pack(padx=padx, pady=pady)

There are almost certainly better ways to solve your actual problem, but for the contrived example in the question the best you can do is use the padx and pady parameters in each call to pack().