在本程序中, 我们来看一下容器嵌套. 我们会创建一组容器, 一个嵌套一个: bottom_frame, left_frame和right_frame.
这些容器里啥控件也没有. 因为容器会自动缩放, 所以它里面没控件时, 就会缩得完全看不到. 但我们可以通过"height"和"width"属性来给它们设置一个初始大小.
注意, 我们并没有为所有的容器设置大小. 比如myContainer1, 我们就没有设置. 但我们将它所有的子容器都设置了大小, 所以它的大小会随着子容器的变化而变化.
在之前的程序中, 我们都把控件塞到容器中, 但在本程序中, 我们只创建容器, 并且给他们设置不同的大小, 位置和背景色.
我们还会给每个容器添加一个边框, 这会让我们很好的区分出它们: bottom_frame, left_frame和right_frame. 而其他的容器(如top_frame和buttons_frame)则不添加这个边框.
*代码示例*
程序运行时, 你会看到一堆不同背景色的容器.
- from Tkinter import *
-
-
class MyApp:
-
def __init__(self, parent):
-
-
self.myParent = parent
-
-
### Our topmost frame is called myContainer1
-
self.myContainer1 = Frame(parent) ###
-
self.myContainer1.pack()
-
-
#------ constants for controlling layout ------
-
button_width = 6 ### (1)
-
-
button_padx = "2m" ### (2)
-
button_pady = "1m" ### (2)
-
-
buttons_frame_padx = "3m" ### (3)
-
buttons_frame_pady = "2m" ### (3)
-
buttons_frame_ipadx = "3m" ### (3)
-
buttons_frame_ipady = "1m" ### (3)
-
# -------------- end constants ----------------
-
-
### We will use VERTICAL (top/bottom) orientation inside myContainer1.
-
### Inside myContainer1, first we create buttons_frame.
-
### Then we create top_frame and bottom_frame.
-
### These will be our demonstration frames.
-
-
# buttons frame
-
self.buttons_frame = Frame(self.myContainer1) ###
-
self.buttons_frame.pack(
-
side=TOP, ###
-
ipadx=buttons_frame_ipadx,
-
ipady=buttons_frame_ipady,
-
padx=buttons_frame_padx,
-
pady=buttons_frame_pady,
-
)
-
-
# top frame
-
self.top_frame = Frame(self.myContainer1)
-
self.top_frame.pack(side=TOP,
-
fill=BOTH,
-
expand=YES,
-
) ###
-
-
# bottom frame
-
self.bottom_frame = Frame(self.myContainer1,
-
borderwidth=5, relief=RIDGE,
-
height=50,
-
background="white",
-
) ###
-
self.bottom_frame.pack(side=TOP,
-
fill=BOTH,
-
expand=YES,
-
) ###
-
-
-
### Now we will put two more frames, left_frame and right_frame,
-
### inside top_frame. We will use HORIZONTAL (left/right)
-
### orientation within top_frame.
-
-
# left_frame
-
self.left_frame = Frame(self.top_frame, background="red",
-
borderwidth=5, relief=RIDGE,
-
height=250,
-
width=50,
-
) ###
-
self.left_frame.pack(side=LEFT,
-
fill=BOTH,
-
expand=YES,
-
) ###
-
-
-
### right_frame
-
self.right_frame = Frame(self.top_frame, background="tan",
-
borderwidth=5, relief=RIDGE,
-
width=250,
-
)
-
self.right_frame.pack(side=RIGHT,
-
fill=BOTH,
-
expand=YES,
-
) ###
-
-
-
# now we add the buttons to the buttons_frame
-
self.button1 = Button(self.buttons_frame, command=self.button1Click)
-
self.button1.configure(text="OK", background= "green")
-
self.button1.focus_force()
-
self.button1.configure(
-
width=button_width, ### (1)
-
padx=button_padx, ### (2)
-
pady=button_pady ### (2)
-
)
-
-
self.button1.pack(side=LEFT)
-
self.button1.bind("", self.button1Click_a)
-
-
self.button2 = Button(self.buttons_frame, command=self.button2Click)
-
self.button2.configure(text="Cancel", background="red")
-
self.button2.configure(
-
width=button_width, ### (1)
-
padx=button_padx, ### (2)
-
pady=button_pady ### (2)
-
)
-
-
self.button2.pack(side=RIGHT)
-
self.button2.bind("", self.button2Click_a)
-
-
def button1Click(self):
-
if self.button1["background"] == "green":
-
self.button1["background"] = "yellow"
-
else:
-
self.button1["background"] = "green"
-
-
def button2Click(self):
-
self.myParent.destroy()
-
-
def button1Click_a(self, event):
-
self.button1Click()
-
-
def button2Click_a(self, event):
-
self.button2Click()
-
-
-
root = Tk()
-
myapp = MyApp(root)
-
root.mainloop()
阅读(965) | 评论(0) | 转发(0) |