Snerler
Snerler

Reputation: 41

Change width of tkinter Entry box to match width of window

from tkinter import *

def show_entry_fields():
    e2.insert(10,(e1.get()))

master = Tk()
master.minsize(width=900, height=20)

Label(master, text="Paste").grid(row=0)
Label(master, text="Output").grid(row=1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)


Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Convert', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)    

mainloop( )

I just started learning Python and tkinter. I want to make the two Entry boxes as wide as the entire window. But even as I make the window larger, the Entry fields are confined to the first column of the "grid" and do not expand. How can I make the entry fields wider?

Upvotes: 3

Views: 8861

Answers (2)

Shruti Srivastava
Shruti Srivastava

Reputation: 398

I have a similar example you can try this:

from Tkinter import *



root = Tk()

row1 = Frame(root)
row1.grid(row=0, column=0, sticky=N+S+E+W)

row2 = Frame(root)
row2.grid(row=1, column=0, sticky=N+S+E+W)

Label1 = Label(row1,text = "First")
Label2 = Label(row2,text = "Second")

entry1 = Entry(row1)
entry2= Entry(row2)

row1.pack(fill=X)
Label1.pack(side = LEFT)
entry1.pack(side = LEFT,fill=X,expand = True)
row2.pack(fill=X)
Label2.pack(side = LEFT)
entry2.pack(side = LEFT,fill=X,expand = True)


root.mainloop()

Upvotes: 0

Eric Levieil
Eric Levieil

Reputation: 3574

From http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html

w.columnconfigure(N, option=value, ...)

weight To make a column or row stretchable, use this option and supply a value that gives the relative weight of this column or row when distributing the extra space. For example, if a widget w contains a grid layout, these lines will distribute three-fourths of the extra space to the first column and one-fourth to the second column:

w.columnconfigure(0, weight=3)
w.columnconfigure(1, weight=1)

If this option is not used, the column or row will not stretch.

So in your case

master.columnconfigure(1, weight=1)

along with updating sticky attribute of e1 (and e2), as pointed out by @BryanOakley:

e1.grid(row=0, column=1, sticky=W+E)

should do the trick.

Upvotes: 5

Related Questions