Python.use(better,Tkinter)《余録》Pack
‖記事一覧‖《こちらに移動中です》2006年11月29日 (水)
Python.use(better, Tkinter)
Pack《Python3.1》
《著》森こねこ・小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第0版♪1993/11/25 ● 第1版♪2006/10/28
概要
詰物 Pack を使って部品を配置する方法を紹介します。
Tkinter によるオブジェクト指向プログラミングへの扉を開きます。
※ Tcl/Tk で作成した例題を、Tkinter で再構成しました。
部品 Pack:side=
次のコードを実行すると、ウィンドウが現われます。
>>> ex_pack_side()
- 親部品の上側(TOP)から順に、子部品を配置します。
- 親部品の下側(BOTTOM)から順に、子部品を配置します。
- 親部品の左側(LEFT)から順に、子部品を配置します。
- 親部品の右側(RIGHT)から順に、子部品を配置します。
def ex_pack_side(): root = Tk() root.title("pack(side=)") root.geometry("240x180") def Box(master, n, color): frame = Frame(master) wh = 40 wid = Frame(frame, width=wh, height=wh, bg=color) wid.grid(row=0, column=0) wid = Label(frame, text=n) wid.grid(row=0, column=0) return frame dim = 2; bd = 2 s = TOP, BOTTOM, LEFT, RIGHT, for i, e in enumerate(s): ry, cx = i//dim, i%dim frame = Frame(root, borderwidth=bd, relief=RIDGE, bg="yellow", ) frame.grid(sticky="NEWS", row=ry, column=cx) w = Label(frame, text=e) w.pack(side=e) colors = "blue", "red", for n, color in enumerate(colors): w = Box(frame, n+1, color) w.pack(side=e) root.mainloop()
《Note》
class Pack: # /Python-3.0/Lib/tkinter/__init__.py """Geometry manager Pack. def pack_configure(self, cnf={}, **kw): pack = configure = config = pack_configure
- フレームには、任意の widget を格納できます。
side= TOP 上側から順に部品を配置します BOTTOM 下側から順に部品を配置します LEFT 左側から順に部品を配置します RIGHT 右側から順に部品を配置します
部品 Pack:fill=
次のコードを実行すると、ウィンドウが現われます。
>>> ex_pack_fill()
- fill=X を指定すると、赤の領域を左右(横方向)に伸展します
- fill=Y を指定すると、青の領域を上下(縦方向)に伸展します
- fill=BOTH を指定すると、赤/青の領域をともに伸展します
def ex_pack_fill(): root = Tk() root.title("pack(fill=)") root.geometry("240x240") def Box(master, fill): parent = Frame(master) child = Frame(parent, bg="white") child.grid(row=0, column=0) for wh, bg, side in [ (10, "red" , TOP ), (20, "blue", LEFT ), (40, "cyan", RIGHT), ]: wid = Frame(child, width=wh, height=wh, bg=bg) wid.pack(side=side, fill=fill) wid = Label(parent, text=fill, bg="yellow") wid.grid(sticky="NEWS", row=1, column=0) return parent dim = 2; bd = 2 s = NONE, X, Y, BOTH, for i, fill in enumerate(s): ry, cx = i//dim, i%dim frame = Frame(root, bd=bd, relief=RIDGE, bg="yellow", ) frame.grid(sticky="NEWS", row=ry, column=cx) w = Box(frame, fill) w.grid(row=ry, column=cx) for width, height, bg, row, column in [ (10,100,"blue",0,2), (10,100,"blue",1,2), (100,10,"red" ,2,0), (100,10,"red" ,2,1), ]: frame = Frame(root, width=width, height=height, bd=bd, relief=RIDGE, bg=bg, ) frame.grid(row=row, column=column) root.mainloop()
《Note》
class Pack: # /Python-3.0/Lib/tkinter/__init__.py """Geometry manager Pack. def pack_configure(self, cnf={}, **kw): pack = configure = config = pack_configure
- フレームには、任意の widget を格納できます。
fill= NONE なにもしません X 横(左右)方向に部品を詰めて配置します Y 縦(上下)方向に部品を詰めて配置します BOTH 全(上下左右)方向に部品を詰めて配置します