ivan199415
ivan199415

Reputation: 438

How to get current height and width of a cell in Tkinter module of Python

I want to get current size of a cell in tkinter's grid packaging. Not just default, but after resizing as well. How do I do that?

Upvotes: 1

Views: 2640

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

You can use the grid_bbox method to get the size of a cell or group of cells.

For example, given a frame f which uses grid to manage its children, the width and height of row 4, column 5 can be retrieved like this:

x, y, width, height = f.grid_bbox(5, 4)

This is how the bbox method is described in the official tcl/tk documentation:

With no arguments, the bounding box (in pixels) of the grid is returned. The return value consists of 4 integers. The first two are the pixel offset from the master window (x then y) of the top-left corner of the grid, and the second two integers are the width and height of the grid, also in pixels. If a single column and row is specified on the command line, then the bounding box for that cell is returned, where the top left cell is numbered from zero. If both column and row arguments are specified, then the bounding box spanning the rows and columns indicated is returned.

Upvotes: 3

Related Questions