1

When I change the OptionMenu It all shift's to the left , and I want everything to stay centered?

It changes it every time I change widget w2.

Code:

from Tkinter import *

root=Tk()
root.geometry("540x250+400+250")
root.title('Converter')
root.resizable(0, 0)

variable = StringVar(root)
variable.set("   Select A Measurement   ") # default value

variable2 = StringVar(root)
variable2.set("Select A Second Measurement") # default value

l=Label(root,text='To')
l.grid(row=1, column=1, sticky='NW')

w = OptionMenu(root, variable, "CM", "MM", "M", "Inches")
w.grid(row=1, column=0, sticky='NE')

w2 = OptionMenu(root, variable2, "Inches", "MM", "M", "CM")
w2.grid(row=1, column=2, sticky='NW')

#spacers
w = Label(root,text='  ')
w.grid(row=3, column=1, sticky='NW')

#spacers
w = Label(root,text='  ')
w.grid(row=2, column=1, sticky='NW')

b = Button(root, text="  Convert  ",font=(None,15))
b.grid(row=4, column=1, sticky='NW')

mainloop()
TheEvil_potato
  • 33
  • 1
  • 1
  • 7

1 Answers1

0

Problem is Grid are flexible if we dont want them to move we need to use .grid_columnconfigure(1, weight=100,minsize=150) minsize to stop strinking

From docs clearly stated that :

Both "columnconfigure" and "rowconfigure" also take a "minsize" grid option, which specifies a minimum size which you really don't want the column or row to shrink beyond.

from Tkinter import *

root=Tk()
root.geometry("540x250+400+250")
root.title('Converter')
root.resizable(0, 0)

frame = Frame(root)
frame.grid(row=0, column=0, sticky='NSEW')


frame.grid_columnconfigure(0,minsize=180 )
frame.grid_columnconfigure(1, weight=100,minsize=150)
frame.grid_columnconfigure(2, weight=100,minsize=150)
frame.grid_columnconfigure(3, weight=100,minsize=150)

variable = StringVar(root)
variable.set("   Select A Measurement   ") # default value

variable2 = StringVar(root)
variable2.set("Select A Second Measurement") # default value

l=Label(frame,text='To')
l.grid(row=1, column=1, sticky='NW',)


w = OptionMenu(frame, variable, "CM", "MM", "M", "Inches")
w.grid(row=1, column=0, sticky='NE')

w2 = OptionMenu(frame, variable2, "Inches", "MM", "M", "CM")
w2.grid(row=1, column=2, sticky='NW')

#spacers
w = Label(frame,text='  ')
w.grid(row=3, column=1, sticky='NW')

#spacers
w = Label(frame,text='  ')
w.grid(row=2, column=1, sticky='NW')

b = Button(frame, text="  Convert  ",font=(None,15))
b.grid(row=4, column=1, sticky='NW')

mainloop()
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46