What does self.grid() do?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Mar 4 04:40:43 CET 2009


On Tue, 03 Mar 2009 18:06:56 -0800, chuck wrote:

> I am learning python right now.  In the lesson on tkinter I see this
> piece of code
> 
> from Tkinter import *
> 
> class MyFrame(Frame):
>    def __init__(self):
>        Frame.__init__(self)
>        self.grid()
> 
> My question is what does "self.grid()" do?  I understand that the grid
> method registers widgets with the geometry manager and adds them to the
> frame

Not "the frame" but the container widget that is the parent of the widget 
on which you call `grid()`.  In this case that would be a (maybe 
implicitly created) `Tkinter.Tk` instance, because there is no explicit 
parent widget set here.  Which IMHO is not a good idea.

And widgets that layout themselves in the `__init__()` are a code smell 
too.  No standard widget does this, and it takes away the flexibility of 
the code using that widget to decide how and where it should be placed.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list