Advertisement
here2share

# Tk_side_fill_expand_anchor.py

Jan 3rd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.12 KB | None | 0 0
  1. # Tk_side_fill_expand_anchor.py
  2.  
  3. from Tkinter import *
  4.  
  5. class MyApp:
  6.     def __init__(self, parent):
  7.        
  8.         #------ constants for controlling layout of buttons ------
  9.         button_width = 6           
  10.         button_padx = "2m"    
  11.         button_pady = "1m"    
  12.         buttons_frame_padx =  "3m"    
  13.         buttons_frame_pady =  "2m"         
  14.         buttons_frame_ipadx = "3m"    
  15.         buttons_frame_ipady = "1m"  
  16.         # -------------- end constants ----------------
  17.  
  18.  
  19.         # set up Tkinter variables, to be controlled by the radio buttons
  20.         self.button_name   = StringVar()
  21.         self.button_name.set("C")
  22.        
  23.         self.side_option = StringVar()
  24.         self.side_option.set(LEFT)
  25.        
  26.         self.fill_option   = StringVar()
  27.         self.fill_option.set(NONE)
  28.  
  29.         self.expand_option = StringVar()
  30.         self.expand_option.set(YES)
  31.        
  32.         self.anchor_option = StringVar()       
  33.         self.anchor_option.set(CENTER)
  34.        
  35.        
  36.         # -------------- end constants ----------------
  37.        
  38.         self.myParent = parent
  39.         self.myParent.geometry("640x400")
  40.  
  41.         ### Our topmost frame is called myContainer1
  42.         self.myContainer1 = Frame(parent) ###
  43.         self.myContainer1.pack(expand=YES, fill=BOTH)
  44.  
  45.  
  46.         ### We will use HORIZONTAL (left/right) orientation inside myContainer1.
  47.         ### Inside myContainer1, we create control_frame and demo_frame.
  48.        
  49.         # control frame - basically everything except the demo frame
  50.         self.control_frame = Frame(self.myContainer1) ###
  51.         self.control_frame.pack(side=LEFT, expand=NO,  padx=10, pady=5, ipadx=5, ipady=5)  
  52.        
  53.         # inside control_frame we create a header label
  54.         # and a buttons_frame at the top,
  55.         # and demo_frame at the bottom
  56.        
  57.         myMessage="This window shows the effects of the \nexpand, fill, and anchor packing options.\n"
  58.         Label(self.control_frame, text=myMessage, justify=LEFT).pack(side=TOP, anchor=W)
  59.        
  60.         # buttons frame
  61.         self.buttons_frame = Frame(self.control_frame) ###
  62.         self.buttons_frame.pack(side=TOP, expand=NO, fill=Y, ipadx=5, ipady=5)    
  63.  
  64.         # demo frame
  65.         self.demo_frame = Frame(self.myContainer1) ###
  66.         self.demo_frame.pack(side=RIGHT, expand=YES, fill=BOTH)            
  67.  
  68.  
  69.         ### Inside the demo frame, we create top_frame and bottom_frame.
  70.         ### These will be our demonstration frames.                        
  71.         # top frame
  72.         self.top_frame = Frame(self.demo_frame)
  73.         self.top_frame.pack(side=TOP, expand=YES, fill=BOTH)  ###    
  74.  
  75.         # bottom frame
  76.         self.bottom_frame = Frame(self.demo_frame,
  77.             borderwidth=5,  relief=RIDGE,
  78.             height=50,
  79.             bg="cyan",
  80.             ) ###  
  81.         self.bottom_frame.pack(side=TOP, fill=X)    
  82.  
  83.  
  84.         ### Now we will put two more frames, left_frame and right_frame,
  85.         ### inside top_frame.  We will use HORIZONTAL (left/right)
  86.         ### orientation within top_frame.
  87.        
  88.         # left_frame       
  89.         self.left_frame = Frame(self.top_frame, background="red",
  90.             borderwidth=5,  relief=RIDGE,
  91.             width=50,
  92.             ) ###      
  93.         self.left_frame.pack(side=LEFT, expand=NO, fill=Y)    
  94.  
  95.  
  96.         ### right_frame
  97.         self.right_frame = Frame(self.top_frame, background="tan",
  98.             borderwidth=5,  relief=RIDGE,
  99.             width=250
  100.             )
  101.         self.right_frame.pack(side=RIGHT, expand=YES, fill=BOTH)    
  102.  
  103.  
  104.         # now put a button in each of the interesting frames
  105.         button_names = ["A", "B", "C"] 
  106.         side_options = [LEFT, TOP, RIGHT, BOTTOM]  
  107.         fill_options = [X, Y, BOTH, NONE]
  108.         expand_options = [YES, NO]
  109.         anchor_options = [NW, N, NE, E, SE, S, SW, W, CENTER]
  110.        
  111.    
  112.         self.buttonA = Button(self.bottom_frame, text="A")
  113.         self.buttonA.pack()
  114.         self.buttonB = Button(self.left_frame, text="B")
  115.         self.buttonB.pack()
  116.         self.buttonC = Button(self.right_frame, text="C")
  117.         self.buttonC.pack()
  118.         self.button_with_name = {"A":self.buttonA, "B":self.buttonB, "C":self.buttonC} 
  119.  
  120.         # now we some subframes to the buttons_frame
  121.         self.button_names_frame   = Frame(self.buttons_frame, borderwidth=5)
  122.         self.side_options_frame   = Frame(self.buttons_frame, borderwidth=5)
  123.         self.fill_options_frame   = Frame(self.buttons_frame, borderwidth=5)
  124.         self.expand_options_frame = Frame(self.buttons_frame, borderwidth=5)
  125.         self.anchor_options_frame = Frame(self.buttons_frame, borderwidth=5)
  126.  
  127.         self.button_names_frame.pack(  side=LEFT, expand=YES, fill=Y, anchor=N)
  128.         self.side_options_frame.pack(  side=LEFT, expand=YES, anchor=N)    
  129.         self.fill_options_frame.pack(  side=LEFT, expand=YES, anchor=N)
  130.         self.expand_options_frame.pack(side=LEFT, expand=YES, anchor=N)
  131.         self.anchor_options_frame.pack(side=LEFT, expand=YES, anchor=N)
  132.                    
  133.         Label(self.button_names_frame, text="\nButton").pack()
  134.         Label(self.side_options_frame, text="Side\nOption").pack()
  135.         Label(self.fill_options_frame, text="Fill\nOption").pack()
  136.         Label(self.expand_options_frame, text="Expand\nOption").pack()
  137.         Label(self.anchor_options_frame, text="Anchor\nOption").pack()     
  138.        
  139.         for option in button_names:
  140.             button = Radiobutton(self.button_names_frame, text=str(option), indicatoron=1,
  141.                 value=option, command=self.button_refresh, variable=self.button_name)
  142.             button["width"] = button_width
  143.             button.pack(side=TOP)
  144.  
  145.         for option in side_options:
  146.             button = Radiobutton(self.side_options_frame, text=str(option), indicatoron=0,
  147.                 value=option, command=self.demo_update, variable=self.side_option)
  148.             button["width"] = button_width
  149.             button.pack(side=TOP)
  150.                                
  151.         for option in fill_options:
  152.             button = Radiobutton(self.fill_options_frame, text=str(option), indicatoron=0,
  153.                 value=option, command=self.demo_update, variable=self.fill_option)
  154.             button["width"] = button_width
  155.             button.pack(side=TOP)
  156.  
  157.         for option in expand_options:
  158.             button = Radiobutton(self.expand_options_frame, text=str(option), indicatoron=0,
  159.                 value=option, command=self.demo_update, variable=self.expand_option)
  160.             button["width"] = button_width
  161.             button.pack(side=TOP)
  162.    
  163.         for option in anchor_options:
  164.             button = Radiobutton(self.anchor_options_frame, text=str(option), indicatoron=0,
  165.                 value=option, command=self.demo_update, variable=self.anchor_option)
  166.             button["width"] = button_width
  167.             button.pack(side=TOP)
  168.  
  169.         self.cancelButtonFrame = Frame(self.button_names_frame)
  170.         self.cancelButtonFrame.pack(side=BOTTOM, expand=YES, anchor=SW)
  171.        
  172.         self.cancelButton = Button(self.cancelButtonFrame,
  173.             command=self.cancelButtonClick,
  174.             text="Cancel", background="red",
  175.             width=button_width,  
  176.             padx=button_padx,    
  177.             pady=button_pady      
  178.             )              
  179.         self.cancelButton.pack(side=BOTTOM, anchor=S)
  180.        
  181.  
  182.         self.cancelButton.bind("<Return>", self.cancelButtonClick)
  183.        
  184.         # set up the buttons in their initial position
  185.         self.demo_update()
  186.  
  187.  
  188.     def button_refresh(self):
  189.         button = self.button_with_name[self.button_name.get()]
  190.         properties = button.pack_info()
  191.         self.fill_option.set  (  properties["fill"] )
  192.         self.side_option.set  (  properties["side"] )
  193.         self.expand_option.set(  properties["expand"] )
  194.         self.anchor_option.set(  properties["anchor"] )
  195.  
  196.  
  197.     def demo_update(self):
  198.         button = self.button_with_name[self.button_name.get()]
  199.         button.pack(fill=self.fill_option.get()
  200.             , side=self.side_option.get()
  201.             , expand=self.expand_option.get()
  202.             , anchor=self.anchor_option.get()
  203.             )
  204.        
  205.        
  206.     def cancelButtonClick(self):
  207.         self.myParent.destroy()      
  208.  
  209.  
  210. root = Tk()
  211. myapp = MyApp(root)
  212. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement