How to set where extra padding goes in tkinter

310 views Asked by At

I have some Python tkinter code with a label that changes length and spans various columns. As it changes length, the extra padding seems to be shared among the columns it spans, whereas I want it to be added to just one column. As an example in the following simple interface, I'd like all the extra padding to be added to column 2 so that as the spinbox increases, the 'Move me' label is moved to the right, but the 'Keep me still' label stays where it is. Please could someone show me how to make this work? Preferably without classes (yes, I am aware that I need to learn about them soon :P)

from tkinter import *
from tkinter import ttk

gui = Tk()

toplabel = StringVar()
value = StringVar()

def combofunc(*args):
    toplabel.set('* ' * 10 * int(value.get()))

ttk.Label(gui, textvariable=toplabel).grid(column=1, columnspan=3, row=1, sticky=W)
Spinbox(gui, from_=1, to=100, textvariable=value, command=combofunc).grid(column=1, row=2, sticky=W)
ttk.Label(gui, text='Keep me still').grid(column=2, row=2, sticky=W)
ttk.Label(gui, text='Move me').grid(column=3, row=2, sticky=W)

for child in gui.winfo_children():
    child.grid_configure(padx=5, pady=5)

combofunc()
gui.mainloop()

EDIT: I meant I want the padding to be added either on the right hand side of column 2, or between column 2 and column 3.

1

There are 1 answers

0
Terry Jan Reedy On BEST ANSWER

You need to configure the columns with expansion weights. Add the following somewhere.

for i, j in ((1,0), (2,1), (3,0)):
    gui.columnconfigure(i, weight=j)

I used this tkinter doc