Python Tkinter module Grid layout manager Parameter Details, tkintergrid

Source: Internet
Author: User

Python Tkinter module Grid layout manager Parameter Details, tkintergrid

When using the Tkinter module to compile the image interface, pack () and grid () are often used for layout management. pack () has fewer parameters and is convenient to use. It is the simplest layout, however, when there are a large number of controls, you may need to use grid () for layout (do not use grid () and pack () in the same window at the same time ()!!).

Grid usage and main parameters:

  • If no parameter is specified, the default value starts from 0.
  • The row number and column number are not as strict as they are on the coordinate axis, but represent a relationship between top, bottom, and left, such:

Label (master, text = "First"). grid (row = 0)
Label (master, text = "Second"). grid (row =1)

And (when the row number "1" does not appear ):

Label (master, text = "First"). grid (row = 0)
Label (master, text = "Second"). grid (row =2)

The results are the same:

2. columnspan: set the number of columns that the cell spans horizontally, that is, the number of columns (width) occupied by the control; rowspan: set the number of rows that the cell spans vertically, that is, the number of rows (height) occupied by the control ).

3. ipadx: Set the horizontal blank area size in the control; ipady: Set the vertical blank area size in the control;

Padx: set the size of the blank area in the horizontal direction around the control; pady: set the size of the blank area in the vertical direction around the control;

4. sticky: the default control alignment in the window is centered. You can use the sticky option to specify the alignment mode. The options include N/S/E/W, which respectively represent the upper/lower/left/right alignment, you can use N/S/E/W separately or both up and down and left and right to achieve different alignment Effects, such:

  

from tkinter import *master = Tk()master.geometry('300x130')e1 = Button(master,text='First',height=2,width=7,fg='red')e2 = Button(master,text='Second',height=3,width=13,fg='blue')e1.grid(row=0, column=0)e2.grid(row=0, column=1)mainloop()
View Code

There are two buttons in the window, the width and height are different, you can see that their alignment is center display:

If you want them to align up and down on the same line, you can set sticky = N + S, meaning that both the top and the low end must be aligned:

  

from tkinter import *master = Tk()master.geometry('300x130')e1 = Button(master,text='First',height=2,width=7,fg='red')e2 = Button(master,text='Second',height=3,width=13,fg='blue')e1.grid(row=0, column=0,sticky=N+S)e2.grid(row=0, column=1,sticky=N+S)mainloop()
View Code

Summary:

Sticky = N/S/E // W: ToP/bottom/right/left

Sticky = N + S: Stretch height to align the top and bottom horizontally

Sticky = E + W. Stretch the width to align the left and right boundary in the vertical direction.

Sticky = N + S + E: Stretch height, align it horizontally, and place the control on the right (when the two controls are placed on the same row and the same column, the effect is obvious)

.......

There are also such combinations as adjusted as needed.

In the Tkinter module, the size and measurement units of various controls are not the same, and may need to be adjusted repeatedly to achieve the best effect.

Note: The above program code applies to Python 3.4.3 and may need to be slightly changed.

  

 

 

 

 

 

 

        

 

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.