pack() メソッドによって、ウィジェットをウインドウに配置することができます。
anchor
expand
fill
ipadx
ipady
padx
pady
side
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='button1')
b1.pack(fill='both')
b2 = Tkinter.Button(root, text='button2')
b2.pack(fill='x')
b3 = Tkinter.Button(root, text='button3')
b3.pack(fill='y')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='button1')
b1.pack(side='left')
b2 = Tkinter.Button(root, text='button2')
b2.pack(side='left')
b3 = Tkinter.Button(root, text='button3')
b3.pack(side='left')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='button1')
b1.pack(fill='both')
b2 = Tkinter.Button(root, text='button2')
b2.pack(fill='both')
b3 = Tkinter.Button(root, text='button3')
b3.pack(fill='both')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='button1')
b1.pack(fill='both', side='left')
b2 = Tkinter.Button(root, text='button2')
b2.pack(fill='both', side='left')
b3 = Tkinter.Button(root, text='button3')
b3.pack(fill='both', side='left')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='button1')
b1.pack(fill='both', side='left')
b2 = Tkinter.Button(root, text='button2')
b2.pack(fill='both', side='left')
b3 = Tkinter.Button(root, text='button3')
b3.pack(fill='both', side='left', expand = True)
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b2 = Tkinter.Button(root, text='north east')
b2.pack(anchor = 'ne')
b8 = Tkinter.Button(root, text='north west')
b8.pack(anchor = 'nw')
b6 = Tkinter.Button(root, text='south west')
b6.pack(anchor = 'sw')
b4 = Tkinter.Button(root, text='south east')
b4.pack(anchor = 'se')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
b1 = Tkinter.Button(root, text='north')
b1.pack(anchor = 'n')
b2 = Tkinter.Button(root, text='east')
b2.pack(anchor = 'e')
b3 = Tkinter.Button(root, text='west')
b3.pack(anchor = 'w')
b4 = Tkinter.Button(root, text='south')
b4.pack(anchor = 's')
root.mainloop()
Frame を使用して、ウィジェットをグループ化することができます。
グループ化されたウィジェットは、一つのウィジェットのように扱うことができます。
主なオプションには、以下の様なものがあります。
flat
raised
sunken
groove
ridge
borderwidth
import Tkinter
root = Tkinter.Tk()
f0 = Tkinter.Frame(root)
f1 = Tkinter.Frame(root)
b1 = Tkinter.Button(f0, text = 'button1')
b1.pack(side = 'left')
b2 = Tkinter.Button(f0, text = 'button2')
b2.pack(side = 'left')
b3 = Tkinter.Button(f0, text = 'button3')
b3.pack(side = 'left')
b4 = Tkinter.Button(f1, text = 'button4')
b4.pack( fill = 'both')
b5 = Tkinter.Button(f1, text = 'button5')
b5.pack( fill = 'both')
b6 = Tkinter.Button(f1, text = 'button6')
b6.pack( fill = 'both')
f0.pack()
f1.pack(fill = 'both')
root.mainloop()
import Tkinter
root = Tkinter.Tk()
f0 = Tkinter.Frame(root, width = 50, height = 50, relief = 'flat', borderwidth = 4, bg = 'gray')
f0.pack(padx = 5, pady = 5, side = 'left')
f1 = Tkinter.Frame(root, width = 50, height = 50, relief = 'raised', borderwidth = 4, bg = 'gray')
f1.pack(padx = 5, pady = 5, side = 'left')
f2 = Tkinter.Frame(root, width = 50, height = 50, relief = 'sunken', borderwidth = 4, bg = 'gray')
f2.pack(padx = 5, pady = 5, side = 'left')
f3 = Tkinter.Frame(root, width = 50, height = 50, relief = 'groove', borderwidth = 4, bg = 'gray')
f3.pack(padx = 5, pady = 5, side = 'left')
f4 = Tkinter.Frame(root, width = 50, height = 50, relief = 'ridge', borderwidth = 4, bg = 'gray')
f4.pack(padx = 5, pady = 5, side = 'left')
root.mainloop()
LabelFrame は、ラベルつきの Frame です。
import Tkinter
root = Tkinter.Tk()
f0 = Tkinter.LabelFrame(root, text = 'flat', width = 100, height = 100, relief = 'flat', borderwidth = 4)
f0.pack(padx = 5, pady = 5, side = 'left')
f1 = Tkinter.LabelFrame(root, text = 'raised', width = 100, height = 100, relief = 'raised', borderwidth = 4)
f1.pack(padx = 5, pady = 5, side = 'left')
f2 = Tkinter.LabelFrame(root, text = 'sunken', width = 100, height = 100, relief = 'sunken', borderwidth = 4)
f2.pack(padx = 5, pady = 5, side = 'left')
f3 = Tkinter.LabelFrame(root, text = 'groove', width = 100, height = 100, relief = 'groove', borderwidth = 4)
f3.pack(padx = 5, pady = 5, side = 'left')
f4 = Tkinter.LabelFrame(root, text = 'ridge', width = 100, height = 100, relief = 'ridge', borderwidth = 4)
f4.pack(padx = 5, pady = 5, side = 'left')
root.mainloop()
ウィジェットで、lower() メソッドを使用してウィジェットを隠すことができます。
import Tkinter
root = Tkinter.Tk()
def hide():
b1.lower()
def show():
f.lower()
f = Tkinter.Frame(root)
f.pack(fill = 'x')
b1 = Tkinter.Button(text = 'hide', command = hide)
b1.pack(in_ = f, fill = 'x')
b2 = Tkinter.Button(root, text = 'show', command = show)
b2.pack(fill = 'x')
root.mainloop()
b1.pack(in_ = f, fill = 'x')
b1 = Tkinter.Button(f, text = 'hide', command = hide)
b1.pack(fill = 'x')