Pack for tkinter tutorials

Source: Internet
Author: User

This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1813077

'''Tkinter pack '''
# Pack as a layout manager, which can be considered as an elastic container
'''1. An empty widget '''
# Do not use pack
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
# View the child components under the current root. The interpreter does not report an exception, indicating that the pack has been created and can be used. The output at this time is empty, that is, the root does not have any child components.
Print root. pack_slaves ()
# Pack a label to the root user
Label (root, text = 'package'). Pack ()
# Print out the root sub-component again. You can see that a sub-component is included, that is, the created label. This indicates that the label is added to the root by calling pack.
Print root. pack_slaves ()
Root. mainloop ()
# Pack_salves: prints the child components of the current component package. You can use this function to check whether each component has an inclusion relationship.
'''2. Relationship between root and pack '''
#-*-Coding: cp936 -*-
# Use the text create_text
From tkinter import *
Root = TK ()
# Change the root size to 80x80
Root. Geometry ('80x80 + 0 + 0 ')
Print root. pack_slaves ()
Label (root, text = 'package'). Pack ()
Print root. pack_slaves ()
Root. mainloop ()
# It can be seen that the result of the pack has not changed, and it does not affect the root. That is to say, the pack can be reduced to only one label component, and the root can control its own size.
''' 3. Add multiple components to the pack '''
#-*-Coding: cp936 -*-
# Add multiple labels to the 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 = 'package' + STR (I). Pack ()
Print root. pack_slaves ()
Root. mainloop ()
# Use the default pack to add components down. The first component is placed at the top of the list, and then arranged down. Note that the display of the last label is incomplete.
'''4. Fixed to free variation '''
# In the above example, label4 is not displayed completely
#-*-Coding: cp936 -*-
# Do not set the root size. Use the default value.
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 = 'package' + STR (I). Pack ()
Print root. pack_slaves ()
Root. mainloop ()
# Use the default pack to add components down. The first component is placed at the top of the list, and then arranged down. In this case, the last one has been shown, which is why pack is called an elastic container. Although it has this feature, it is not always able to layout according to our meaning, we can forcibly set the container size to overwrite the default setting of the pack. The priority of a pack is low.
'''5. How does fill control the layout of child components '''
#-*-Coding: cp936 -*-
# Do not set the root size. Use the default value.
From tkinter import *
Root = TK ()
# Change the root size to 80x80
Root. Geometry ('80x80 + 0 + 0 ')
Print root. pack_slaves ()
# Create three labels and use different fill attributes.
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 must be filled in the Y direction, the second one must be filled in the XY direction, and the third one does not use the fill attribute, note that the pack will only provide the smallest area that can accommodate the three components. It does not allow the remaining space, so there is a "blank" at the bottom ".
'''6. Expand how to control the layout of components '''
#-*-Coding: cp936 -*-
# This attribute specifies how to use extra space, that is, the "blank" left in the previous example"
From tkinter import *
Root = TK ()
# Change the root size to 80x80
Root. Geometry ('80x80 + 0 + 0 ')
Print root. pack_slaves ()
# Create three labels and use different fill attributes.
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 must be filled in the Y direction, the second one must be filled in the XY direction, and the third one does not use the fill attribute, in this example, the first label and the second label use the expand = 1 attribute, and the third uses the expand = 0 attribute to change the root size, we can see that label1 and label2 change with the size of the root (its available space is strictly changing), and the third only uses fill to fill in the X direction, no extra space is used.
'''7. Change the Emission Location of the component '''
# Use the side attribute to change the placement location
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
# Change the root size to 80x80
Root. Geometry ('80x80 + 0 + 0 ')
Print root. pack_slaves ()
# Create three labels and use different fill attributes for horizontal placement.
# Place the first label to the left
Label (root, text = 'pack1', BG = 'red'). Pack (fill = Y, expand = 1, side = left)
# Place the second label on the right
Label (root, text = 'pack2', BG = 'blue'). Pack (fill = both, expand = 1, side = right)
# Place the third label on the left rather than the label. Note that the label will not be placed on the left of label1.
Label (root, text = 'pack3', BG = 'green'). Pack (fill = x, expand = 0, side = left)
Print root. pack_slaves ()
Root. mainloop ()
# The first one must be filled in the Y direction, the second one must be filled in the XY direction, and the third one does not use the fill attribute, in this example, the first label and the second label use the expand = 1 attribute, and the third uses the expand = 0 attribute to change the root size, we can see that label1 and label2 change with the size of the root (its available space is strictly changing), and the third only uses fill to fill in the X direction, no extra space is used.
'''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 value.
From tkinter import *
Root = TK ()
# Change the root size to 80x80
# Root. Geometry ('80x80 + 0 + 0 ')
Print root. pack_slaves ()
# Create three labels and use different fill attributes for horizontal placement.
# Place the first labelframe to the left
L1 = labelframe (root, text = 'pack1', BG = 'red ')
# Set ipadx 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 ()
# To demonstrate ipadx/padx, A labelframe is created to set ipadx to 20, that is, the internal interval value is 20. If its sub-component is used, 20 units are set; label2 and label3 respectively set the external Interval Values in the X and Y directions, and all components arranged with them will keep the distance of 10 unit values.

Pack for tkinter tutorials

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.