Using a specific number of pixels of spacing between each Button
doesn't sound to me like such as good idea because it isn't very flexible nor easily portable to devices with different resolutions.
Nevertheless I've figured-out a way of doing it—namely by putting a do-nothing invisible button between of the each real ones. This got somewhat involved, mostly because it requires putting an image on each Button
used this way so its width
option argument will be interpreted as number of pixels instead of number of characters (here's some documentation describing the various Button
widget configuration options).
import tkinter as tk# Inline XBM format data for a 1x1 pixel image.BITMAP = """ #define im_width 1 #define im_height 1 static char im_bits[] = { 0x00 };"""root = tk.Tk()root.geometry("960x600")bitmap = tk.BitmapImage(data=BITMAP, maskdata=BITMAP)f1 = tk.Frame(root, width=70, height=30)f1.grid(row=3, column=0, sticky=tk.EW)def banana(): print ("Sundae")def tomato(): print ("Ketchup")def potato(): print ("Potato chips")def layout_buttons(parent, buttons, spacing): if buttons: first, *rest = buttons first.grid(row=0, column=0) # Position first Button. for index, button in enumerate(rest, start=1): col = 2*index # Dummy widget to separate each button from the one before it. separator = tk.Button(parent, relief=tk.FLAT, state=tk.ACTIVE, image=bitmap, borderwidth=0, highlightthickness=0, width=spacing) separator.grid(row=0, column=col-1) button.grid(row=0, column=col)buttons = ( tk.Button(f1, text="Banana", command=banana), tk.Button(f1, text="Tomato", command=tomato), tk.Button(f1, text="Potato", command=potato),)layout_buttons(f1, buttons, 30)root.mainloop()
Result:
Here's a blow-up showing that the spacing is exactly 30 pixels (as counted in my image editor and indicated by the thin horizontal black line between the adjacent edges of the two Button
s).