# 容器控件
###### tags: `活用tkinter – 學習運用GUI`
## 框架
* 先試著弄出框架
```python=
from tkinter import *
root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.config(bg = "lightgreen")
for fm in ["red", "green", "lightblue"]:
Frame(root, bg = fm, height = 200, width = 500).pack()
root.mainloop()
```
* 結果

* 加上滑鼠花樣 :three_button_mouse:
```python=
fms = {"red":"cross", "green":"boat", "blue":"clock"}
for fmcolor in fms:
Frame(root, bg = fmcolor, cursor = fms[fmcolor],
height = 200, width = 200).pack(side = LEFT)
```
* 結果

## 活用relief屬性
```python=
fm1 = Frame(width = 200, height = 200, relief = GROOVE, borderwidth = 5)
fm1.pack(side = LEFT, padx = 5, pady = 10)
fm2 = Frame(width = 200, height = 200, relief = RAISED, borderwidth = 5)
fm2.pack(side = LEFT, padx = 5, pady = 10)
fm3 = Frame(width = 200, height = 200, relief = RIDGE, borderwidth = 5)
fm3.pack(side = LEFT, padx = 5, pady = 10)
```
* 結果

## 建構內部核取方塊
```python=
fm = Frame(width = 200, height = 200, relief = RAISED, borderwidth = 5)
lab = Label(fm, text = "程式語言")
lab.pack()
py = Checkbutton(fm, text = "Python")
py.pack(anchor = W)
c = Checkbutton(fm, text = "c")
c.pack(anchor = W)
rb = Checkbutton(fm, text = "ruby")
rb.pack(anchor = W)
js = Checkbutton(fm, text = "javascript")
js.pack(anchor = W)
fm.pack(padx = 10, pady = 10)
```
* 結果

## 標籤框架
* 也是一個容器widget控件 :arrow_heading_down:
* 將剛才的程式碼修改 :point_down:
```python=
from tkinter import *
def prlan():
sel = ""
for i in check:
if check[i].get() == True:
sel += lan[i] + "\t"
print(sel)
root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.config(bg = "lightgreen")
labFrame = LabelFrame(root, text = "程式語言")
lan = {0 : "pyhton", 1 : "c", 2 : "ruby", 3 : "java"}
check = {}
for i in range(len(lan)):
check[i] = BooleanVar()
Checkbutton(labFrame, text = lan[i],
variable = check[i]).grid(row = i + 1, sticky = W)
labFrame.pack(ipadx = 5, ipady = 10)
btn = Button(root, text = "check", width = 10, command = prlan)
btn.pack()
root.mainloop()
```
* 結果

## 頂層視窗
* toplevel的基本概念
* 類似於Frame,但它是獨一無二的視窗
```python=
from tkinter import *
root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.config(bg = "lightgreen")
top1 = Toplevel()
Label(top1, text = "測試用").pack()
root.mainloop()
```
* 結果

## 做出模擬對話框效果
```python=
from tkinter import *
import random
def msgbox():
msgtype = random.randint(1,3)
if msgtype == msgyes:
labTxt = "yes"
elif msgtype == msgno:
labTxt = "no"
else:
labTxt = "Exit"
top1 = Toplevel()
top1.geometry("500x500")
top1.title("測試")
Label(top1, text = labTxt).pack(fill = X, expand = True)
root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.config(bg = "lightgreen")
msgyes, msgno, msgexit = 1, 2, 3
btn = Button(root, text = "點擊", command = msgbox)
btn.pack()
root.mainloop()
```
* 結果

{%hackmd S1DMFioCO %}