Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter.grid spacing options?

I'm new to Tkinter, and I tried creating an app with the grid layout manager. However, I can't seem to find a way to utilize it the way I want to. What I need to do is simulate a grid full of 'cells' so that I can place, for example, a label in cell (3,8) or a button in cell (5,1). Here is an example of what I've tried:

import tkinter as tk
root = tk.Tk()

label = tk.Label(root, text = 'Label')
label.grid(column = 3, row = 8)

button = tk.Button(root, text = 'Button')
button.grid(column = 5, row = 1)

Tkinter keeps the relative position of each widget (i.e. button is above and to the right of label), but it ignores any space in between. I've searched a bit for this problem and found out about weight, but that doesn't seem to be what I'm looking for; I'd like something independent of the label and button's layouts. Thanks for the help!

like image 758
PlatypusVenom Avatar asked Jan 19 '15 06:01

PlatypusVenom


People also ask

What does grid () do in Tkinter?

tkinter Tkinter Geometry Managers grid() The grid() geometry manager organises widgets in a table-like structure in the parent widget. The master widget is split into rows and columns, and each part of the table can hold a widget. It uses column , columnspan , ipadx , ipady , padx , pady , row , rowspan and sticky .

What is PADX and Pady in Python?

padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders. row − The row to put widget in; default the first row that is still empty.

Is Pack or grid better Tkinter?

pack is the easiest layout manager to use with Tkinter. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the layout manager of choice.


1 Answers

You can specify the spacing on columns/rows in the grid (including those that contain no widget) by running the grid_columnconfigure and grid_rowconfigure methods on the parent of the widgets (in this case, the parent would be root).

For example, to set a minimum width for the 4th column, add the last line to your code:

import Tkinter as tk
root = tk.Tk()

label = tk.Label(root, text = 'Label')
label.grid(column = 3, row = 8)

button = tk.Button(root, text = 'Button')
button.grid(column = 5, row = 1)

root.grid_columnconfigure(4, minsize=100)  # Here

If you want to set a minimum size for all of your columns and rows, you can iterate through them all using:

col_count, row_count = root.grid_size()

for col in xrange(col_count):
    root.grid_columnconfigure(col, minsize=20)

for row in xrange(row_count):
    root.grid_rowconfigure(row, minsize=20)

Edit Interestingly, effbot states:

minsize=Defines the minimum size for the row. Note that if a row is completely empty, it will not be displayed, even if this option is set.

(italics mine) but it seems to work even for rows and columns that are empty, as far as I've tried.

like image 192
zehnpaard Avatar answered Sep 23 '22 17:09

zehnpaard