tkinter, annoying grid-problem

Bryan Oakley oakley at bardo.clearlight.com
Sun Apr 13 09:45:50 EDT 2008


skanemupp at yahoo.se wrote:
> so my little calculator works perfectly now. just having some trouble
> with the layout.
> this whole tkinter-thing seems to be more tricky than it should be.
> how can i make the 4 column of buttons have the same distance and
> size  between them as the other 3 columns?
> and how can i make the top entry end where the 2nd row entry
> ends(meaning the top entry will be longer)?
> 
> why are the 4th row split from the others? hard to fix the problems
> when u dont even understand why things happen. seems so llogical a lot
> of it. i change something then something unexpected happens.

The best answer I can give (being a Tk expert but not yet a tkinter 
expert) is to start with a piece of graph paper. Draw the GUI out and 
you'll probably see what the problems are. For one, the second entry 
(for the answer) spans 4 columns and begins at column 3, so it ends up 
in column 6. This ends up affecting the whole layout because nothing 
else goes to column six.

You'll probably find it much easier going to break down your GUI into 
sections. One for the calculator buttons and one for everything else. 
Create a frame for the buttons and it becomes trivial to layout out all 
the buttons in a 4x6 grid, unaffected by things outside that grid. Then, 
create your other widgets and grid the whole frame of buttons as a 
single unit inside the outermost frame. I quite often use grid for 
interior groupings, then use pack on the outter-most frame to manager 
the various groups.

Using the grid layout manager is trivial if you do a little thinking and 
planning up front. If you don't, you can spend all day chasing down why 
you end up with an extra blank row or column, unusually sized rows and 
columns, etc. Again, get a piece of graph paper and draw it out -- that 
helps immensely when you're first coming up to speed using grid.





More information about the Python-list mailing list