"""Show the effect of the fill and expand options to pack. Don't look at this source -- just look at the effects. """ from Tkinter import * import string class App: def __init__(self, parent): self.parent = parent self.create_widgets() def create_widgets(self): self.create_view() self.create_controls() def create_view(self): self.f_view = Frame(self.parent, width=300, height=200, relief=SUNKEN, borderwidth=2) self.f_view.pack() self.f_view.propagate(0) self.top = Button(self.f_view, text="TOP", relief=GROOVE) self.top.pack(fill=BOTH) self.t1 = Button(self.f_view, text="T1", relief=GROOVE) self.t1.pack() self.t2 = Button(self.f_view, text="T2 (anchor=SW)", anchor=SW, relief=GROOVE) self.t2.pack() self.bottom = Button(self.f_view, text="BOTTOM", relief=GROOVE) self.bottom.pack(side=BOTTOM, fill=BOTH) self.targets = [self.t1, self.t2] def create_controls(self): self.v_expand = BooleanVar() self.v_fill = StringVar() self.v_fill.set(NONE) self.v_anchor = StringVar() self.v_anchor.set(CENTER) self.f_controls = Frame(self.parent) self.f_controls.pack(fill=X) self.b_expand = Checkbutton(self.f_controls, text="Expand", variable=self.v_expand, command=self.set_it, anchor=W) self.b_expand.pack(fill=X) self.f_fill = Frame(self.f_controls) self.f_fill.pack(fill=X) self.l_fill = Label(self.f_fill, text="Fill:", anchor=W) self.l_fill.pack(side=LEFT, fill=X) # This could be done in a loop, hey! self.b_fillnone = Radiobutton(self.f_fill, text="NONE", variable=self.v_fill, value=NONE, command=self.set_it) self.b_fillnone.pack(side=LEFT) self.b_fillx = Radiobutton(self.f_fill, text="X", variable=self.v_fill, value=X, command=self.set_it) self.b_fillx.pack(side=LEFT) self.b_filly = Radiobutton(self.f_fill, text="Y", variable=self.v_fill, value=Y, command=self.set_it) self.b_filly.pack(side=LEFT) self.b_fillboth = Radiobutton(self.f_fill, text="BOTH", variable=self.v_fill, value=BOTH, command=self.set_it) self.b_fillboth.pack(side=LEFT) self.f_anchor = Frame(self.f_controls) self.f_anchor.pack(fill=X) self.l_anchor = Label(self.f_anchor, text="Anchor:", anchor=W) self.l_anchor.pack(side=LEFT, fill=X) # This could be done in a loop, hey! self.b_anchorcenter = Radiobutton( self.f_anchor, text="CENTER", variable=self.v_anchor, value=CENTER, command=self.set_it) self.b_anchorcenter.pack(side=LEFT) self.b_anchorsw = Radiobutton( self.f_anchor, text="NE", variable=self.v_anchor, value=NE, command=self.set_it) self.b_anchorsw.pack(side=LEFT) # Don't show all 9 anchor options... self.show = Label(self.f_controls, anchor=W, justify=LEFT) self.show.pack(fill=X) self.set_it() def set_it(self): expand = self.v_expand.get() fill = self.v_fill.get() anchor = self.v_anchor.get() text = "w.pack(expand=%d,\nfill=%s, anchor=%s)" % ( expand, string.upper(fill), string.upper(anchor)) self.show.config(text=text) for w in self.targets: w.pack(expand=expand, fill=fill, anchor=anchor) # Boilerplate from here on... if __name__ == '__main__': root = Tk() app = App(root) root.mainloop()