70.md 1.9 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8
# Tkinter 框架

> 原文: [https://pythonbasics.org/tkinter_frame/](https://pythonbasics.org/tkinter_frame/)

Tk 中的框架可让您组织和分组小部件。 它像一个容器一样工作。 它是可以放置遗物的矩形区域。

如果您制作的是 GUI 应用,则将使用其他小部件。 这些小部件需要以某种方式进行组织,这就是框架的来源。

W
wizardforcel 已提交
9

W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
### Tkinter 框架按钮
W
init  
wizardforcel 已提交
12

W
wizardforcel 已提交
13
下面的 Tkinter 程序演示了框架的使用。 它包括带有回调功能的按钮。 框架可以有填充。
W
init  
wizardforcel 已提交
14

W
wizardforcel 已提交
15
```py
W
init  
wizardforcel 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
from tkinter import *

def say_hi():
    print("hello ~ !")

root = Tk()

frame1 = Frame(root)
frame2 = Frame(root)
root.title("tkinter frame")

label= Label(frame1,text="Label",justify=LEFT)
label.pack(side=LEFT)

hi_there = Button(frame2,text="say hi~",command=say_hi)
hi_there.pack()

frame1.pack(padx=1,pady=1)
frame2.pack(padx=10,pady=10)

root.mainloop()

```

![tkinter frame button](img/a4ad0ac569db24ffce3a2765c083ff3e.jpg)

W
wizardforcel 已提交
42
### Tkinter 框架照片
W
init  
wizardforcel 已提交
43

W
wizardforcel 已提交
44
可以添加不同类型的小部件。 该示例具有 Tkinter 框架照片。 它还有一个标签。 您可以向框架添加任何类型的小部件。
W
init  
wizardforcel 已提交
45

W
wizardforcel 已提交
46
```py
W
init  
wizardforcel 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
from tkinter import *

root = Tk()

textLabel = Label(root,
                  text="Label",
                  justify=LEFT,
                  padx=10)
textLabel.pack(side=LEFT)

photo = PhotoImage(file="cat.png")
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)

mainloop()

```

![tkinter frame photo](img/b846c5f8fde20fcdeef56fff17542aef.jpg)

W
wizardforcel 已提交
67
### Tkinter 框架
W
init  
wizardforcel 已提交
68

W
wizardforcel 已提交
69
下面的 Tkinter 程序添加了不同颜色的多个框架。 它们都具有相同的宽度和高度。 换句话说,您可以更改框架的样式。
W
init  
wizardforcel 已提交
70

W
wizardforcel 已提交
71
```py
W
init  
wizardforcel 已提交
72 73 74 75 76 77 78 79 80 81 82 83
from tkinter import *  
root = Tk()  

for fm in ['blue','red','yellow','green','white','black']:  
    Frame(height = 20,width = 640,bg = fm).pack()  
root.mainloop() 

```

![tkinter frame](img/6698c6cc40057eb1f4c8f9fe8432c7d0.jpg)

[下载 Tkinter 示例](https://gum.co/ErLc)