Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71982
  • 博文数量: 26
  • 博客积分: 628
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 11:57
文章分类

全部博文(26)

文章存档

2012年(4)

2011年(19)

2010年(3)

我的朋友

分类: Python/Ruby

2011-04-29 13:19:04

在本程序中, 我们来看一下容器嵌套. 我们会创建一组容器, 一个嵌套一个: bottom_frame, left_frame和right_frame.

这些容器里啥控件也没有. 因为容器会自动缩放, 所以它里面没控件时, 就会缩得完全看不到. 但我们可以通过"height"和"width"属性来给它们设置一个初始大小.

注意, 我们并没有为所有的容器设置大小. 比如myContainer1, 我们就没有设置. 但我们将它所有的子容器都设置了大小, 所以它的大小会随着子容器的变化而变化.

在之前的程序中, 我们都把控件塞到容器中, 但在本程序中, 我们只创建容器, 并且给他们设置不同的大小, 位置和背景色.

我们还会给每个容器添加一个边框, 这会让我们很好的区分出它们: bottom_frame, left_frame和right_frame. 而其他的容器(如top_frame和buttons_frame)则不添加这个边框.

*代码示例*

程序运行时, 你会看到一堆不同背景色的容器.

  1. from Tkinter import *

  2. class MyApp:
  3.     def __init__(self, parent):
  4.         
  5.         self.myParent = parent

  6.         ### Our topmost frame is called myContainer1
  7.         self.myContainer1 = Frame(parent) ###
  8.         self.myContainer1.pack()

  9.         #------ constants for controlling layout ------
  10.         button_width = 6 ### (1)
  11.         
  12.         button_padx = "2m" ### (2)
  13.         button_pady = "1m" ### (2)

  14.         buttons_frame_padx = "3m" ### (3)
  15.         buttons_frame_pady = "2m" ### (3)
  16.         buttons_frame_ipadx = "3m" ### (3)
  17.         buttons_frame_ipady = "1m" ### (3)
  18.         # -------------- end constants ----------------

  19.         ### We will use VERTICAL (top/bottom) orientation inside myContainer1.
  20.         ### Inside myContainer1, first we create buttons_frame.
  21.         ### Then we create top_frame and bottom_frame.
  22.         ### These will be our demonstration frames.
  23.         
  24.         # buttons frame
  25.         self.buttons_frame = Frame(self.myContainer1) ###
  26.         self.buttons_frame.pack(
  27.             side=TOP, ###
  28.             ipadx=buttons_frame_ipadx,
  29.             ipady=buttons_frame_ipady,
  30.             padx=buttons_frame_padx,
  31.             pady=buttons_frame_pady,
  32.             )
  33.         
  34.         # top frame
  35.         self.top_frame = Frame(self.myContainer1)
  36.         self.top_frame.pack(side=TOP,
  37.             fill=BOTH,
  38.             expand=YES,
  39.             ) ###

  40.         # bottom frame
  41.         self.bottom_frame = Frame(self.myContainer1,
  42.             borderwidth=5, relief=RIDGE,
  43.             height=50,
  44.             background="white",
  45.             ) ###
  46.         self.bottom_frame.pack(side=TOP,
  47.             fill=BOTH,
  48.             expand=YES,
  49.             ) ###


  50.         ### Now we will put two more frames, left_frame and right_frame,
  51.         ### inside top_frame. We will use HORIZONTAL (left/right)
  52.         ### orientation within top_frame.
  53.         
  54.         # left_frame
  55.         self.left_frame = Frame(self.top_frame, background="red",
  56.             borderwidth=5, relief=RIDGE,
  57.             height=250,
  58.             width=50,
  59.             ) ###
  60.         self.left_frame.pack(side=LEFT,
  61.             fill=BOTH,
  62.             expand=YES,
  63.             ) ###


  64.         ### right_frame
  65.         self.right_frame = Frame(self.top_frame, background="tan",
  66.             borderwidth=5, relief=RIDGE,
  67.             width=250,
  68.             )
  69.         self.right_frame.pack(side=RIGHT,
  70.             fill=BOTH,
  71.             expand=YES,
  72.             ) ###


  73.         # now we add the buttons to the buttons_frame    
  74.         self.button1 = Button(self.buttons_frame, command=self.button1Click)
  75.         self.button1.configure(text="OK", background= "green")
  76.         self.button1.focus_force()
  77.         self.button1.configure(
  78.             width=button_width, ### (1)
  79.             padx=button_padx, ### (2)
  80.             pady=button_pady ### (2)
  81.             )

  82.         self.button1.pack(side=LEFT)
  83.         self.button1.bind("", self.button1Click_a)
  84.         
  85.         self.button2 = Button(self.buttons_frame, command=self.button2Click)
  86.         self.button2.configure(text="Cancel", background="red")
  87.         self.button2.configure(
  88.             width=button_width, ### (1)
  89.             padx=button_padx, ### (2)
  90.             pady=button_pady ### (2)
  91.             )
  92.     
  93.         self.button2.pack(side=RIGHT)
  94.         self.button2.bind("", self.button2Click_a)
  95.         
  96.     def button1Click(self):
  97.         if self.button1["background"] == "green":
  98.             self.button1["background"] = "yellow"
  99.         else:
  100.             self.button1["background"] = "green"
  101.     
  102.     def button2Click(self):
  103.         self.myParent.destroy()
  104.         
  105.     def button1Click_a(self, event):
  106.         self.button1Click()
  107.                 
  108.     def button2Click_a(self, event):
  109.         self.button2Click()


  110. root = Tk()
  111. myapp = MyApp(root)
  112. root.mainloop()
阅读(965) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~