user1077071
user1077071

Reputation: 961

Python Tkinter Grid Manager and Entry Widget

This is my code so far:

master = Tk()
Label(master, text="Input:").grid(row=0)

e1 = Entry(master, width = 100)
e1.grid(row=0, column=1)

Button(master, text='Q', command=q_pressed).grid(row=3, column=2,
                                                sticky=W, padx = 4, pady=4)

Button(master, text='C', command =c_input).grid(row=3, column=1,
                                                sticky=W, padx = 0, pady=4)

Button(master, text='Confirm', command=parse_input).grid(row=3, column=0,
                                                sticky=W, padx = 4, pady=4)

I am trying to make it so that "Q" button is next to the "C" button (like how "C" is next to the "Confirm" button) but it instead places it where the Entry widget ends.

I understand this is a grid management issue. How do I set up a layout so row = 0, has text and entry widget and row=3 has its own independent columns (0,1,2) for the buttons?

Upvotes: 3

Views: 27469

Answers (3)

James
James

Reputation: 21

enter code here{ master = Tk() Label(master, text="Input:" ).grid(row=0)

e1 = Entry(master, width=100)
e1.grid(row=0, column=1, columnspan=10)

Button(master, text='Q', width=5, command=q_pressed).grid(row=3, column=1, padx=0, ipadx=5, pady=4, sticky=E)

Button(master, text='C', width=5, command =c_input).grid(row=3, column=1, padx=0, ipadx=5, pady=4, sticky=W)

Button(master, text='Confirm', width=10, command=parse_input).grid(row=3, column=0, padx=0, pady=4, sticky=W)

mainloop()}

Since you wanted 100 char for entry string I based my cell/column width on 10 for simple math division. Set my columnspan to 10. I got rid of padx since it pertains to border width and used ipadx which pertains to inner text width. Made entry set to 10 columns wide of 10 char. Used "input" text as 1 column. This will allow me to use 10 char or one column for text "confirm" and I can break column 1 in half by making button "c" & "q" 5 char wide using width=5. Then by using justfy put "c" West and "q" East.

There are many ways this is just easiest in my mind.

Upvotes: 0

mmgp
mmgp

Reputation: 19221

There is possibly more than one way to perform the layout you want, I choose to present a clean one. Your Entry widget occupies a large horizontal space in the second column of the first row, thus placing widgets below it in the next row is going to cause you trouble. Since you know you will have 3 buttons below it, you could make this Entry span two columns (columnspan=2 while gridding it) and play with that while gridding the next row. But I prefer a different approach, which also produces a better layout overall. What you want is to grid a Frame in the second row that takes two columns (since in the row above it you have two widgets). Then you grid your buttons on this new Frame.

This turns your code into:

import Tkinter

master = Tkinter.Tk()

Tkinter.Label(master, text="Input:").grid(row=0, column=0)
Tkinter.Entry(master, width = 100).grid(row=0, column=1)

frame = Tkinter.Frame()
frame.grid(row=1, column=0, columnspan=2, sticky='w')
Tkinter.Button(frame, text='Confirm').grid(row=0, column=0, sticky='w')
Tkinter.Button(frame, text='C').grid(row=0, column=1, sticky='w')
Tkinter.Button(frame, text='Q').grid(row=0, column=2, sticky='w')

master.mainloop()

Upvotes: 2

Holger
Holger

Reputation: 2175

Your problem is that the Entry e1 is very long and contains 100 characters, but it spans only one column. You can use columnspan to define the number of used columns for e1 in the grid command. Following example with columnspan = 30 works for me. Decide yourself, what would be a reasonable value for columnspan.

from Tkinter import *
master = Tk()
Label(master, text="Input:").grid(row=0)

e1 = Entry(master, width = 100)
e1.grid(row=0, column=1, columnspan=30)

Button(master, text='Q').grid(row=3, column=2)

Button(master, text='C').grid(row=3, column=1)

Button(master, text='Confirm').grid(row=3, column=0)
mainloop()

Upvotes: 4

Related Questions