What does self.grid() do?

chuck charles.leviton at gmail.com
Tue Mar 10 20:52:00 EDT 2009


On Mar 5, 3:03 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Wed, 04 Mar 2009 09:04:50 -0800, chuck wrote:
> > On Mar 3, 10:40 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> >> 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.
>
> > I think I understand what you're saying! How would you recommend I go
> > about this?  How do I create an explicit parent?
>
> You create it and pass it as argument to child widgets.
>
> import Tkinter as tk
>
> class MyFrame(tk.Frame):
>     def __init__(self, parent):
>         tk.Frame.__init__(self, parent)
>
> > What exactly is meant by "widgets that layout themselves"- what is the
> > right way to do this?
>
> Call one of the three layout methods on the widget instance after you
> created it, and not in the `__init__()` of the widget.
>
> Your example above "grids" itself at its parent widget, I think at the
> next free cell on a grid if you don't give the position as argument.  
> There is no chance to use another layout manager or to place it in
> another cell.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Wow- lots of good answers and directions- let me go off and digest
this.
Thanks Marc and "r".



More information about the Python-list mailing list