Re: What does self.grid() do?

2009-03-10 Thread chuck
On Mar 5, 3:03 am, Marc 'BlackJack' Rintsch  wrote:
> On Wed, 04 Mar 2009 09:04:50 -0800, chuck wrote:
> > On Mar 3, 10:40 pm, Marc 'BlackJack' Rintsch  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".
--
http://mail.python.org/mailman/listinfo/python-list


Re: What does self.grid() do?

2009-03-04 Thread Marc 'BlackJack' Rintsch
On Wed, 04 Mar 2009 09:04:50 -0800, chuck wrote:

> On Mar 3, 10:40 pm, Marc 'BlackJack' Rintsch  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
--
http://mail.python.org/mailman/listinfo/python-list


Re: What does self.grid() do?

2009-03-04 Thread r
PS: Check here
http://effbot.org/tkinterbook/

There are three geometry managers "pack", "place", and "grid". Be sure
to learn the pros and cons of all three.
--
http://mail.python.org/mailman/listinfo/python-list


Re: What does self.grid() do?

2009-03-04 Thread 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.

--
http://mail.python.org/mailman/listinfo/python-list


Re: What does self.grid() do?

2009-03-04 Thread chuck
On Mar 3, 10:40 pm, Marc 'BlackJack' Rintsch  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.
>
> 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!
--
http://mail.python.org/mailman/listinfo/python-list


Re: What does self.grid() do?

2009-03-03 Thread Marc 'BlackJack' Rintsch
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
--
http://mail.python.org/mailman/listinfo/python-list