What does self.grid() do?

r rt8396 at gmail.com
Wed Mar 4 19:00:29 CET 2009


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




More information about the Python-list mailing list