What does self.grid() do?

C

chuck

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

But in this case am I adding the frame to the frame? Not able to
understand the response from my class discussion board, hence posting
here.

THanks!
 
M

Marc 'BlackJack' Rintsch

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
 
C

chuck

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

I think I understand what you're saying!
How would you recommend I go about this? How do I create an explicit
parent?

What exactly is meant by "widgets that layout themselves"- what is the
right way to do this?

Thanks!
 
R

r

What exactly is meant by "widgets that layout themselves"- what is the
right way to do this?

He means you can't control it at creation time, you would have to call
w.pack_configure() if you did not like the default options. There are
times however when you DO want a widget to pack itself..

from Tkinter import *

class LE(Frame):
def __init__(self, master, text):
Frame.__init__(self, master)
self.label = Label(self, text=text, font=("Courier New", 10))
self.label.pack(side=LEFT)
self.entry = Entry(self)
self.entry.pack(side=LEFT)

class LE2(Frame):
def __init__(self, master, text):
Frame.__init__(self, master)
self.label = Label(self, text=text, font=("Courier New", 10))
self.label.pack(side=LEFT)
self.entry = Entry(self)
self.entry.pack(side=LEFT)
self.pack()

class Win1(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
LE(self, 'name:').pack()
LE(self, ' age:').pack()
self.pack()

class Win2(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
LE(self, 'name:').pack(side=LEFT)
LE(self, ' age:').pack(side=LEFT)
self.pack()

class Win3(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
LE2(self, 'name:')
LE2(self, ' age:')
self.pack()

w1 = Win1()
w1.mainloop()
w2 = Win2()
w2.mainloop()
w3 = Win3()
w3.mainloop()

#-- this time use root --#
root = Tk()
LE(root, 'name:').pack(side=LEFT)
LE(root, ' age:').pack(side=LEFT)
root.mainloop()

It really does not matter because Tkinter is setup to auto create a
root window even if you don't.
 
M

Marc 'BlackJack' Rintsch

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
 
C

chuck

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)


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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top