[Tutor] Tkinter GUI Grid Layout Manager

Kent Johnson kent37 at tds.net
Thu Sep 21 14:02:03 CEST 2006


Johnston Jiaa wrote:
> I am trying to get 3 buttons, equally separated along the top of the 
> window.  How do I get them to all be the same size and how do I make 
> them so that they are equidistant to each other?
> 
> Along with those three buttons, I am trying to get a text box, near the 
> middle of the window.  Every time I attempt to put this in, it messes up 
> the position of the top buttons.

The problem is, when you add the text box, it forces the size of the 
grid columns to change which affects the size and spacing of the 
buttons. The way to avoid that is to put the buttons in their own Frame, 
with its own layout. Here is an example based on your code. I also used 
explicit padding to put space between the buttons:

from Tkinter import *

top = Tk()

# Top "Mode" Widgets in their own panel
# Settings Button
buttonPanel = Frame()
settings_bttn = Button(buttonPanel, text = "Settings")
settings_bttn.grid(row = 0, column=0, padx=10, sticky = EW)

# Statistics Button
stats_bttn = Button(buttonPanel, text = "Statistics")
stats_bttn.grid(row = 0, column=1, padx=10, sticky = EW)

# Procrastinate Button
proc_bttn = Button(buttonPanel, text = "Procrastinate")
proc_bttn.grid(row = 0, column=2, padx=10, sticky = EW)


# Top-Mid Separator
top_mid_sep_lbl = Label(buttonPanel, text = 
"------------------------------------------------------------------------------------------------------")
top_mid_sep_lbl.grid(row = 1, column = 0, columnspan = 3, sticky = EW)

# Top-Mid Separator
top_mid_sep_lbl = Label(buttonPanel, text = 
"------------------------------------------------------------------------------------------------------")
top_mid_sep_lbl.grid(row = 1, column = 0, columnspan = 10, sticky = EW)

buttonPanel.pack(side='top')

# Mid Assignments Widgets
# Assignments Text Display
assign_disp_txt = Text(top, width = 30, height = 18, wrap = WORD)
assign_disp_txt.pack()

top.mainloop()


Kent



More information about the Tutor mailing list