mkHun mkHun - 10 months ago 61
Python Question

Second row entry width affect the first row style in tkinter?

enter image description hereI'm trying to create the some sample application.

Which first row is One label then input entry box then submit button.
Then second row has the another entry box.

My problem is when I increase width of the entry box in second row it affect the first row style. I don't know what is the problem.

import Tkinter
tk_obj = Tkinter.Tk()
tk_geo = tk_obj.geometry("1200x800")
Tkinter.Label(tk_obj, text='Enter query ').grid(row=1,column=1)

def callback():
print "hi"

E1 = Tkinter.Entry(tk_obj,bd=3,width=120)
E1.grid(row=1, column=2,ipady=3)

b = Tkinter.Button(tk_obj, text="Check", command=callback)

E2 = Tkinter.Entry(tk_obj,bd=3,width=100)


Answer Source

The grid method places widgets in the center of the cell they inhabit. When you have two widgets of different sizes sharing a row or column, this means that there will be blank space around the smaller widget. To make the second Entry widget span the first two columns, use columnspan=2 when you grid() it. To left-align it within those two columns, use sticky='W':

E2.grid(row=2,column=1,ipady=100, columnspan=2, sticky='W')

You can then adjust that Entry widget's width attribute until it looks the way you want it to.