Python pack layout

Source: Internet
Author: User

#Pack为一布局管理器, it can be seen as a resilient container
"1. An empty widget"
#不使用pack
#-*-coding:cp936-*-
From Tkinter Imp

ORT *
root = Tk ()
# View the sub-components under current root, the interpreter does not report an exception, stating that the pack has been created and can be used, when the output is empty, that is, Root does not have any child components.
Print root.pack_slaves ()
# Pack a label to root
Label (root,text = ' pack '). Pack ()
# once again, print out the root sub-component and see that it already contains a component, the label you just created, stating that the label Call Pack () is adding itself to root. Print root.pack_slaves ()
Root.mainloop ()
# Pack_salves Prints the subcomponents owned by the current component package, which allows you to see if each component has a containing relationship.for example [<tkinter.label instance at 0x0143f378>] This result

"2.root relationship with Pack "
#-*-coding:cp936-*-
# Use text Create_text
From Tkinter Import *
root = Tk ()
# Change the root size to 80x80
Root.geometry (' 80x80+0+0 ')

#80x80代表了初始化时主窗口的大小, 0, 0 represents the location of the window at initialization time
Print root.pack_slaves ()
Label (root,text = ' pack '). Pack ()
Print root.pack_slaves ()
Root.mainloop ()

#可以看出Pack的结果没有什么变化, it does not affect root, which means that the pack can "shrink" to include only one label component, and Root can control its own size.
"3. Add multiple components to pack '
#-*-coding:cp936-*-
# Add multiple labels to pack
From Tkinter Import *
root = Tk ()
# Change the root size to 80x80
Root.geometry (' 80x80+0+0 ')
Print root.pack_slaves ()
For I in range (5):

Label (root,text = ' pack ' + str (i)). Pack ()
Print root.pack_slaves ()
Root.mainloop ()
# Use the default settings pack to add components down, first at the top, then down in order. Note that the last label display is not complete, because we set the main window size 80x80 is too small, if we change the larger point, 120x120 will be fine.
"4. Fixed set to free change"
# in the example above see Label4 not showing full
#-*-coding:cp936-*-
# do not set the root size, use the default
From Tkinter Import *
root = Tk ()
#去掉下面的这句
#root. Geometry (' 80x80+0+0 ')
Print root.pack_slaves ()
For I in range (5):
Label (root,text = ' pack ' + str (i)). Pack ()

Print root.pack_slaves ()
Root.mainloop ()

the results of print show: [<tkinter.label instance at 0x012de9e0>, <tkinter.label instance at 0x012de990>, < Tkinter.label instance at 0x012dea80>, <tkinter.label instance at 0x012e5828>, <tkinter.label instance at 0x0 12e5878>]

#使用用默认的设置pack将向下添加组件, the first one is at the top, then the bottom is in order. So the last one has been shown, and that's why the pack is called a resilient container, though with this feature,but it's not always able to do what we mean, we can force the size of the container to override the pack's default settings. Pack has a low priority.
"5.fill How to control the layout of sub-components"
#-*-coding:cp936-*-
# do not set the root size, use the default
From Tkinter Import *
root = Tk ()
# Change the root size to 150x150
Root.geometry (' 150x150+0+0 ')
Print root.pack_slaves ()
# Create three labels using a different fill property
Label (root,text = ' Pack1 ', bg = ' red '). Pack (fill = "Y")
Label (root,text = ' Pack2 ', bg = ' blue '). Pack (fill = "both")
Label (root,text = ' Pack3 ', bg = ' green '). Pack (fill = "X")
Print root.pack_slaves ()
Root.mainloop ()
#第一个只保证在Y方向填充, the second guarantee is filled in xy two direction, the third one does not use the Fill attribute, note that pack will only be stingy to give the smallest area that can accommodate the three components, it does not allow the use of the remaining space, it left a "blank" below.

Results:


"6.expand How to control the layout of components"
#-*-coding:cp936-*-
# This property specifies how to use extra space, the "blank" left in the example above
From Tkinter Import *
root = Tk ()
# Change the root size to 150x150
Root.geometry (' 150x150+0+0 ')
Print root.pack_slaves ()
# Create three labels using a different fill property
Label (root,text = ' Pack1 ', bg = ' red '). Pack (fill = "Y", expand = 1)
Label (root,text = ' Pack2 ', bg = ' blue '). Pack (fill = "both", expand = 1)
Label (root,text = ' Pack3 ', bg = ' green '). Pack (fill = "X", expand = 0)
Print root.pack_slaves ()
Root.mainloop ()
# The first one is guaranteed to be filled in the Y direction, the second is guaranteed to be filled in the XY two direction, the third one does not use the Fill attribute, the first label and the second label in this example use the Expand = 1 property, and the third uses the expand = 0 attribute to change the root size, You can see that Label1 and Label2 vary with the size of the root (strictly its free space is changing), and the third only uses fill for the X-direction padding, without extra space.

Look at the results:

you can see how the rest of the space is used.
"7. Change the discharge position of the component"
# Use the side property to change the drop location
#-*-coding:cp936-*-
From Tkinter Import *
root = Tk ()
# Change the root size to 150x150
Root.geometry (' 150x150+0+0 ')
Print root.pack_slaves ()
# Create three labels using a different fill property instead of horizontal placement
# Place the first label on the left
Label (root,text = ' Pack1 ', bg = ' red '). Pack (fill = "Y", expand = 1,side = "Left")
# Place a second label right
Label (root,text = ' Pack2 ', bg = ' blue '). Pack (fill = "both", expand = 1,side = "Right")
# Place the third label on the left and place it on the label, noting that it will not be placed on the left side of Label1
Label (root,text = ' Pack3 ', bg = ' green '). Pack (fill = "X", expand = 0,side = "Left")
Print root.pack_slaves ()
Root.mainloop ()

# The first one is guaranteed to be filled in the Y direction, the second is guaranteed to be filled in the XY two direction, the third one does not use the Fill attribute, the first label and the second label in this example use the Expand = 1 property, and the third uses the expand = 0 attribute to change the root size, You can see that Label1 and Label2 vary with the size of the root (strictly its free space is changing), and the third only uses fill for the X-direction padding, without extra space.

Look at the first one is the size of the original 150x150, the second is the situation after getting bigger


"8. Set the gap size between components"
# IPADX Set Internal clearance
# PADX Set External clearance
#-*-coding:cp936-*-
# do not set the root size, use the default
From Tkinter Import *
root = Tk ()
# Change the root size to 200x150
# root.geometry (' 200x150+0+0 ')
Print root.pack_slaves ()
# Create three labels using a different fill property instead of horizontal placement
# Place the first labelframe on the left
L1 = labelframe (root,text = ' Pack1 ', bg = ' red ')
# Set the Ipadx property to 20
L1.pack (side = LEFT,IPADX = 20)
Label (L1,

Text = ' inside ',
BG = ' Blue '
). Pack (expand = 1,side = left)
L2 = Label (Root,
Text = ' Pack2 ',
BG = ' Blue '
). Pack (fill = Both,expand = 1,side = LEFT,PADX = 10)
L3 = Label (Root,
Text = ' Pack3 ',
BG = ' green '
). Pack (fill = X,expand = 0,side = Left,pady = 10)
Print root.pack_slaves ()

Root.mainloop ()
#为了演示ipadx/PADX, a labelframe is created that sets its IPADX to 20, that is, the internal interval value is 20, and its subcomponents leave 20 units if used, and Label2 and Label3 set the external interval values in the x and y directions, respectively. The distance that all the aligned components retain 10 unit values

Python pack layout

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.