Related content:
- Pack
- Introduced
- Common parameters
- Use case
- Common functions
- Grid
- Introduced
- Common parameters
- Use case
- Common functions
- Place
- Introduced
- Common parameters
- Use case
- Common functions
Starting Time: 2018-03-04 14:20
Pack
- Introduced:
- The pack Geometry Manager packs widgets by row or column. You can control this geometry manager with options such as fill Fill, expand Expand, and pull-over side.
- The form of the pack's emission controls is like putting a control in the same size from top to down
- The window default size is exactly the size of all controls when the window is not set to size and the pack is used for layout
- Controls that are added by default are centered and occupy the entire row
- Common parameters:
- Fill: Fills the space assigned to the control, fill:x to fill the x-axis direction, y for y-axis, and both to X+y
- Expand: First, according to the normal standard to specify the size of the control, if the back window is left space, then will be assigned to those expand=true control, is two different sizes of buttons, 2expand=true, when using expand=true, side will not take effect!
- Side: Side the value Left,right,top,bottom by making the control side-by in the specified direction of the window.
- Anchor: Alignment, desirable value "n", "S", "W", "E", "NW", "SW", "se", "ne", "center" (Default is "center")
- IPADX, Ipady: Inner margin
- Padx,pady: Outer margin
- Applicable: Pack has some limitations compared to gird, but it is much easier to use in some but very common situations:
- Place a widget into a frame (or any other container widget) and have it fill the entire frame
- Put multiple widgets together
- Place some widgets side-by
- Add:
- Common functions:
- pack_propagate (flag): The size of the window when the window is used to configure the pack control, and when flag=0, the window size will no longer just wrap all the controls, when the height and width settings of the window can take effect
- Pack_forget (): Remove the control but not destroy it, you can use pack or another way to display
- Pack_info (): Returns the value of the option provided by the pack.
- Pack_slaves (): Returns all child component objects of this component in a list.
fromTkinterImport*Root=Tk ()#For I in Range (Ten): ##side #btn = Button (text=i) #Btn.pack (side=left) #Btn.pack (side=right) #Btn.pack (side= (top,left)) ## # #fill #Btn.pack (fill=x) #Btn.pack (side=left,fill=y) #Btn.pack (Fill=both)##expand#Btn1=button (text=1,bg= ' green ')#Btn2=button (text=2,bg= ' yellow ')#Btn1.pack (fill=x)#Btn2.pack (expand=true)#Anchor:#Btn1=button (text=1,bg= ' green ')#Btn2=button (text=2,bg= ' yellow ')#Btn1.pack (fill=x)#Btn2.pack (anchor=e)#Pack_propagete#Btn1=button (text=1,bg= ' green ')#Btn2=button (text=2,bg= ' yellow ')#root.pack_propagate (0)#Btn1.pack ()#Btn2.pack ()#Pack_forget:#Import time,threading#Label=label (text= "Warning! ")#Label.pack ()#def run ():#start_time=time.time ()#While True:#if Time.time ()-start_time>3:#Label.pack_forget ()# Break#t=threading. Thread (Target=run)#T.setdaemon (True)#T.start ()Root.mainloop ()
Grid
- Description: Grid is the meaning of the grid, the grid can be placed in block-style control.
- Common parameters:
- Row: line number to distinguish whether different controls are in the same row
- Column number to distinguish whether different controls are in the same column
- Sticky: Determines the snapping direction of the control, which takes one or more values from the set n,s,e,w [cardinal meaning]. For example, to align the label to the left border, you can use W (West)
- RowSpan: Row span, which determines the number of rows that a control occupies, the default row
- ColumnSpan: column span, which determines the number of columns a control occupies, the default column
- IPADX, Ipady: Inner margin
- Padx,pady: Outer margin
- Applicable situation:
- Especially handy when designing dialog boxes.
- When the block is placed
- Add:
- Common functions:
- Grid_forget: Removing the control but not destroying it, you can use grid or another way to show [Grid_remove has the same effect]
Grid_propagete: When the window is resized by the window to configure the pack control, when flag=0, the window size will no longer just wrap all the controls, when the height and width settings of the window can take effect
Grid_slaves (): Returns all child component objects of this component in a list.
fromTkinterImport*Root=Tk () root.title ("my Window") Label_user=label (text='User name:', bg='Green') Label_pwd=label (text='Password:', bg='Green') User=Entry () pwd=Entry ()#Row,column,stickyLabel_user.grid (ROW=0,COLUMN=0,STICKY=W)#one with sticky, one without sticky, to make a distinctionLabel_pwd.grid (row=1,column=0)#Rowspan,columnspanUser.grid (row=0,column=1) Pwd.grid (Row=1,column=1) btn=button (text="Submit") Btn.grid (Row=0,column=3,rowspan=2,columnspan=2,padx=5, pady=5)#The following is mainly to pull the first column to show the effect of the above stickyv=Intvar () Check=checkbutton (text="Next Automatic login", variable=V,) check.grid (Row=2,column=0,sticky=W)#_forget:ImportTime,threadinglabel=label (text="Welcome to login! ", bg='Blue') Label.grid ()defrun (): Start_time=time.time () whileTrue:ifTime.time ()-start_time>3: Label.grid_forget () BreakT=threading. Thread (target=run) T.setdaemon (True) T.start () Root.mainloop ()
Place
- Description: The control can be placed using an absolute position or relative position.
- Common parameters:
- X: The x-coordinate of the control in the window
- Y: the y-coordinate of the control in the window
- Relx,rely: The relative position of the main window, such as
- Relwidth,relheight: Relative size, such as height=0.5,width=0.5, when the control occupies the One-fourth size of the window
- Applicable situation:
- Customizing display methods
- Position the button in the dialog box
- Add:
- Common functions:
- Place_forget (): Removes the control, but does not destroy it, you can use place again or another way to display
- Place_slaves (): Returns all child component objects of this component in a list. [Slaves () has the same function]
- Place_info (): Returns the value corresponding to the option provided by place.
fromTkinterImport*Root=Tk () root.geometry ('100x100') Label=label (text="User name:", bg='Green') Entry=Entry () label.pack (fill=X)#x, y#entry.place (x=0,y=0)#relx,rely#entry.place (relx=0.3, rely=0.3)#entry.place (relx=0.5, rely=0.5)#Relheight,relwidth#entry.place (relx=0.5, rely=0.5,relheight=0.5,relwidth=0.5)#_forget:ImportTime,threadinglabel2=label (text="Warning! ", bg='Blue') Label2.place (Relx=0.2,rely=0.2)defrun (): Start_time=time.time () whileTrue:ifTime.time ()-start_time>3: Label2.place_forget () BreakT=threading. Thread (target=run) T.setdaemon (True) T.start () Root.mainloop ()
To learn more, refer to Tkinter's official documentation: http://effbot.org/tkinterbook/
Python:gui's Tkinter Learning Note 2 interface layout display