Python GUI Tk : Pack
Several pack() options for controlling layouts within a frame: side, fill, expand, anchor
from Tkinter import * class MyApp: def __init__(self, parent): button_width = 6 button_padx = "2m" button_pady = "1m" buttons_frame_padx = "3m" buttons_frame_pady = "2m" buttons_frame_ipadx = "3m" buttons_frame_ipady = "1m" self.button_name = StringVar() self.button_name.set("C") self.side_option = StringVar() self.side_option.set(LEFT) self.fill_option = StringVar() self.fill_option.set(NONE) self.expand_option = StringVar() self.expand_option.set(YES) self.anchor_option = StringVar() self.anchor_option.set(CENTER) self.myParent = parent self.myParent.geometry("640x400") self.myContainer1 = Frame(parent) self.myContainer1.pack(expand=YES, fill=BOTH) self.control_frame = Frame(self.myContainer1) self.control_frame.pack(side=LEFT, expand=NO, padx=10, pady=5, ipadx=5, ipady=5) myMessage="Demo" Label(self.control_frame, text=myMessage, justify=LEFT).pack(side=TOP, anchor=W) self.buttons_frame = Frame(self.control_frame) self.buttons_frame.pack(side=TOP, expand=NO, fill=Y, ipadx=5, ipady=5) self.demo_frame = Frame(self.myContainer1) self.demo_frame.pack(side=RIGHT, expand=YES, fill=BOTH) self.top_frame = Frame(self.demo_frame) self.top_frame.pack(side=TOP, expand=YES, fill=BOTH) self.bottom_frame = Frame(self.demo_frame, borderwidth=5, relief=RIDGE, height=50, bg="cyan", ) self.bottom_frame.pack(side=TOP, fill=X) self.left_frame = Frame(self.top_frame, background="red", borderwidth=5, relief=RIDGE, width=50, ) self.left_frame.pack(side=LEFT, expand=NO, fill=Y) self.right_frame = Frame(self.top_frame, background="tan", borderwidth=5, relief=RIDGE, width=250 ) self.right_frame.pack(side=RIGHT, expand=YES, fill=BOTH) button_names = ["A", "B", "C"] side_options = [LEFT, TOP, RIGHT, BOTTOM] fill_options = [X, Y, BOTH, NONE] expand_options = [YES, NO] anchor_options = [NW, N, NE, E, SE, S, SW, W, CENTER] self.buttonA = Button(self.bottom_frame, text="A") self.buttonA.pack() self.buttonB = Button(self.left_frame, text="B") self.buttonB.pack() self.buttonC = Button(self.right_frame, text="C") self.buttonC.pack() self.button_with_name = {"A":self.buttonA, "B":self.buttonB, "C":self.buttonC} self.button_names_frame = Frame(self.buttons_frame, borderwidth=5) self.side_options_frame = Frame(self.buttons_frame, borderwidth=5) self.fill_options_frame = Frame(self.buttons_frame, borderwidth=5) self.expand_options_frame = Frame(self.buttons_frame, borderwidth=5) self.anchor_options_frame = Frame(self.buttons_frame, borderwidth=5) self.button_names_frame.pack( side=LEFT, expand=YES, fill=Y, anchor=N) self.side_options_frame.pack( side=LEFT, expand=YES, anchor=N) self.fill_options_frame.pack( side=LEFT, expand=YES, anchor=N) self.expand_options_frame.pack(side=LEFT, expand=YES, anchor=N) self.anchor_options_frame.pack(side=LEFT, expand=YES, anchor=N) Label(self.button_names_frame, text="\nButton").pack() Label(self.side_options_frame, text="Side\nOption").pack() Label(self.fill_options_frame, text="Fill\nOption").pack() Label(self.expand_options_frame, text="Expand\nOption").pack() Label(self.anchor_options_frame, text="Anchor\nOption").pack() for option in button_names: button = Radiobutton(self.button_names_frame, text=str(option), indicatoron=1, value=option, command=self.button_refresh, variable=self.button_name) button["width"] = button_width button.pack(side=TOP) for option in side_options: button = Radiobutton(self.side_options_frame, text=str(option), indicatoron=0, value=option, command=self.demo_update, variable=self.side_option) button["width"] = button_width button.pack(side=TOP) for option in fill_options: button = Radiobutton(self.fill_options_frame, text=str(option), indicatoron=0, value=option, command=self.demo_update, variable=self.fill_option) button["width"] = button_width button.pack(side=TOP) for option in expand_options: button = Radiobutton(self.expand_options_frame, text=str(option), indicatoron=0, value=option, command=self.demo_update, variable=self.expand_option) button["width"] = button_width button.pack(side=TOP) for option in anchor_options: button = Radiobutton(self.anchor_options_frame, text=str(option), indicatoron=0, value=option, command=self.demo_update, variable=self.anchor_option) button["width"] = button_width button.pack(side=TOP) self.cancelButtonFrame = Frame(self.button_names_frame) self.cancelButtonFrame.pack(side=BOTTOM, expand=YES, anchor=SW) self.cancelButton = Button(self.cancelButtonFrame, text="Cancel", background="red", width=button_width, padx=button_padx, pady=button_pady ) self.cancelButton.pack(side=BOTTOM, anchor=S) self.cancelButton.bind("<Button-1>", self.cancelButtonClick) self.cancelButton.bind("<Return>", self.cancelButtonClick) self.demo_update() def button_refresh(self): button = self.button_with_name[self.button_name.get()] properties = button.pack_info() self.fill_option.set ( properties["fill"] ) self.side_option.set ( properties["side"] ) self.expand_option.set( properties["expand"] ) self.anchor_option.set( properties["anchor"] ) def demo_update(self): button = self.button_with_name[self.button_name.get()] button.pack(fill=self.fill_option.get() , side=self.side_option.get() , expand=self.expand_option.get() , anchor=self.anchor_option.get() ) def cancelButtonClick(self, event): self.myParent.destroy() root = Tk() myapp = MyApp(root) root.mainloop()
Pack Button to grid
from Tkinter import * import math root = Tk() top = Frame(root) top.pack(side='top') font = 'times 18 bold' hwtext = Label(top, text='Hello, World!', font=font) hwtext.grid(row=0, column=0, columnspan=4, pady=20) r_label = Label(top, text='The sine of') r_label.grid(row=1, column=0) r = StringVar() r.set('1.2') r_entry = Entry(top, width=6, textvariable=r) r_entry.grid(row=1, column=1) s = StringVar() def comp_s(event=None): global s s.set('%g' % math.sin(float(r.get()))) # construct string r_entry.bind('<Return>', comp_s) compute = Button(top, text=' equals ', command=comp_s, relief='flat') compute.grid(row=1, column=2) s_label = Label(top, textvariable=s, width=12) s_label.grid(row=1, column=3) def quit(event=None): root.destroy() quit_button = Button(top, text='Goodbye, GUI World!', command=quit, background='yellow', foreground='blue') quit_button.grid(row=2, column=0, columnspan=4, pady=5, sticky='ew') root.bind('<q>', quit) root.mainloop()
Pack controls
from Tkinter import * import math root = Tk() top = Frame(root) top.pack(side='top') hwframe = Frame(top) hwframe.pack(side='top') font = 'times 18 bold' hwtext = Label(hwframe, text='Hello, World!', font=font) hwtext.pack(side='top', pady=20) rframe = Frame(top) rframe.pack(side='top', padx=10, pady=20) r_label = Label(rframe, text='The sine of') r_label.pack(side='left') r_entry = Entry(rframe, width=6) r_entry.pack(side='left') r_entry.insert('end', '1.2') # insert default value s_label = Label(rframe, width=12) def comp_s(event=None): r = float(r_entry.get()) s = math.sin(r) s_label.configure(text='%g' % s) compute = Button(rframe, text=' equals ', command=comp_s, relief='flat') compute.pack(side='left') s_label.pack(side='left') r_entry.bind('<Return>', comp_s) def quit(event=None): root.destroy() quit_button = Button(top, text='Goodbye, GUI World!', command=quit,background='yellow', foreground='blue') quit_button.pack(side='top', pady=5, fill='x') root.bind('<q>', quit) root.mainloop()
We can do with pack()
from Tkinter import * class MyApp: def __init__(self, parent): self.myContainer1 = Frame(parent) self.myContainer1.pack() self.button1 = Button(self.myContainer1) self.button1["text"]= "Hello, World!" self.button1.pack(side=LEFT) self.button2 = Button(self.myContainer1) self.button2.configure(text="Off to join the circus!") self.button2.pack(side=LEFT) self.button3 = Button(self.myContainer1) self.button3.configure(text="Join me?") self.button3.pack(side=LEFT) self.button4 = Button(self.myContainer1, text="Goodbye!") self.button4.pack(side=LEFT) root = Tk() myapp = MyApp(root) root.mainloop()