Pack of Tkinter Tutorials

Source: Internet
Author: User
' Tkinter Tutorial ' pack Chapter '
# Pack is a layout manager that can be viewed as an elastic container
' 1. An empty widget '
# does not use Pack
#-*-Coding:utf-8-* -From
tkinter import *

root = Tk ()
# View the subcomponents under the current root, the interpreter does not report an exception, the pack is created and can be used, the output is empty, that is, Root does not have any subcomponents.
print (Root.pack_slaves ())
# packs a label
label (Root, text= ' pack ') to root. Pack ()
# Prints the subassembly of root again, You can see that you already have a component, the label you just created, 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 package, which allows you to see if each component has an inclusive relationship.

' 2.root relationship to pack '
#-*-coding:utf-8-*-
# using text create_text from
tkinter import *

root = Tk () 
  # change the size of root to 80x80
root.geometry (' 80x80+0+0 ')
print (Root.pack_slaves ())
Label (Root, text= ' pack ') . Pack ()
print (Root.pack_slaves ())
Root.mainloop ()
# You can see that the result of the pack does not change, it does not affect root, which means the pack can "shrink" To contain only one label component, Root can control its own size.

' 3. Add multiple components to pack '
#-*-Coding:utf-8-*-
# Add multiple labels from
tkinter import *

root = Tk () # to the pack to
change The size of root is 80x80
root.geometry (' 80x80+0+0 ')
print (Root.pack_slaves ()) for the
I in range (5):
    Label (Root, Text= ' pack ' + str (i)). Pack ()
print (Root.pack_slaves ())
Root.mainloop ()
# uses the default settings pack to add components down, The first one is at the top, then the next is the descending order. Note that the last label is not fully displayed and will later explain why

' 4. Fixed set to free change '
# in the example above see Label4 does not show full
#-*-coding:utf-8-*-
# does not set root size, use default from
Tkinter Import *

root = Tk ()
# Remove the following sentence
# 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 a component down, the first one at the top, and then the next. So the last one has been shown,
# This is why the pack is called a flexible container, although there is this feature, but it is not always able to follow our meaning of the layout,
# We can force the size of the container to overwrite the pack default settings. The pack has a low priority.

' 5.fill how to control the layout of subassemblies '
#-*-coding:utf-8-*-
# does not set the size of root, use default from
tkinter import *

root = Tk ()
# Change the size of root to 80x80
root.geometry (' 80x80+0+0 ')
print (Root.pack_slaves ())
# Create three labels using different fill properties
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 ()
# The first one is only guaranteed to be filled in the Y direction, the second guarantee is filled in xy two direction, the third does not use the Fill property,
# Note pack only stingy to give the smallest area that can hold the three components, it does not allow the use of the remaining space, so there is a "blank".

' 6.expand how to control the layout of the component '
#-*-coding:utf-8-*-
# This property specifies how to use the extra space, which is the "blank" from
tkinter import

that is left in the previous example root = Tk ()
# Change the size of root to 80x80
root.geometry (' 80x80+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 only guaranteed to fill in the Y direction, the second is to be filled in xy two, and the third does not use the Fill property,
# The first and second label in this example uses expand = 1 property And the third uses the expand = 0 attribute to change the size of the root,
# you can see that Label1 and Label2 vary with the size of the root (strictly its available space is changing), and the third uses fill in the X-direction and does not use extra space.

' 7. Change the component's emission position '
# Use the side property to change the placement position
#-*-Coding:utf-8-*-from
tkinter import *

root = Tk ()
# change Root Size is 80x80
root.geometry (' 80x80+0+0 ')
print (Root.pack_slaves ())
# creates three labels, uses different fill properties, and changes to horizontal placement
# Place the first label left
label (Root, text= ' pack1 ', bg= ' red '). Pack (Fill=y, expand=1, Side=left)
# Place
the second label right Label (Root, text= ' Pack2 ', bg= ' Blue '). Pack (Fill=both, expand=1, Side=right)
# Place the third label on the left, placed by the label, Note that it will not be placed on the left Label of Label1
(root, text= ' Pack3 ', bg= ' green '). Pack (Fill=x, Expand=0, Side=left)
print (root.pack _slaves ())
Root.mainloop ()
# The first one is only guaranteed to fill in the Y direction, the second one is guaranteed to fill in the XY two direction, the third does not use the Fill property, in this case the first label and the second label use the expand = 1 Properties,
# While the third uses the expand = 0 property to change the size of the root, you can see that Label1 and Label2 vary with the size of the root (strictly its available space is changing),
The third only uses fill in the X-direction padding and does not use extra space.

' 8. Set the gap size between components '
# IPADX set internal Gap
# PADX Set external Gap
#-*-Coding:utf-8-*-
# do not set the size of root, use default from
Tkinter Import *

root = Tk ()
# Change the size of root to 80x80
# root.geometry (' 80x80+0+0 ')
print (Root.pack_slaves ()
# Create three labels using different fill attributes, change to horizontal
# Place the first labelframe left
L1 = Labelframe (root, text= ' pack1 ', bg= ' red ')
# Set IPADX property to
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 ()
# in order to demonstrate IPADX/PADX, a labelframe was created to set its IPADX to 20, that is, the internal interval value,
# Its subassemblies will be set aside in 20 units; Label2 and Label3 separate the values of the outer intervals in the X and Y directions, and all the components arranged with them are separated by a distance of 10 unit values.

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.