Pack of Tkinter Tutorials

Source: Internet
Author: User
Tags pack tkinter tutorial
"' The pack of the Tkinter tutorial '
# pack is a layout manager that can be viewed as a flexible container
' 1. An empty widget '
# Do not use Pack
#-*-coding:cp936-*-
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 ()
# Pack a label to root
Label (root,text = ' pack '). Pack ()
# Print out the subcomponents of root again, and you can see that there is already a component, the label 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 with pack '
#-*-coding:cp936-*-
# Use 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 ()
# can see that the result of the pack does not change, it does not affect root, that is, the pack can be "reduced" to contain only a label component, Root can control its own size.
' 3. Adding multiple components to the pack '
#-*-coding:cp936-*-
# add more than one label to the pack
From Tkinter Import *
root = Tk ()
# change the size of root 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 a component down, the first one at the top, and then the next. 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 that LABEL4 does not show complete
#-*-coding:cp936-*-
# does not set the size of root, using the default
From Tkinter Import *
root = Tk ()
# get rid of 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, and that is why the pack is called a flexible container, although it has this feature, but it is not always able to layout according to our meaning, we can force the size of the container to override the pack's default settings. The pack has a low priority.
"5.fill How to control the layout of subassemblies" "
#-*-coding:cp936-*-
# does not set the size of root, using the 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 with 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 fill in the Y direction, the second guarantee is in xy two direction padding, the third does not use the padding attribute, the note pack only stingy gives the smallest area which can hold these three components, it does not allow to use the remaining space, therefore has "the blank" below.
"6.expand How to control the layout of the component" "

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.