11.3 use other types of sizer [wxpython in action]

Source: Internet
Author: User
11.3 use other types of sizer

We have discussed the basic sizer, and now we can turn to a more complex and more flexible sizer. Two of them (Flex grid sizer and grid bag Sizer) are essentially grid variants. The other two (box and static box Sizer) use a different and more flexible layout structure.

11.3.1 what is Flex grid sizer?

Flex grid sizer is a more flexible version of grid sizer. It is almost the same as the standard grid sizer, except for the following exceptions:

1. Each row and column can have its own size.
2. By default, when the size is adjusted, it does not change the size of its cell. If necessary, you can specify which row or column should grow.
3. It can be flexibly increased in one of the two directions, meaning that you can specify the ratio column for individual child elements, and you can specify the behavior in a fixed direction.

Figure 11.7 shows a flex grid sizer with nine cells in its layout. The middle cell is larger.

Fig 11.7

Compared with figure 11.5, for the same layout, the size of each cell in Figure 11.5 is the same as that of the intermediate object. In Flex grid sizer, the size of a cell is determined by its row and column. Their width is the width of the project with the largest width in the column, and their height is the width of the project with the highest width in the row. Here, the cell height of the project "four" and project "six" is higher than that of the project itself, because the project "five" in its counterparts ", the cell width of "two" and "seven" is wider. "One," "three," "seven," and "Nine" cells are normal sizes and are not affected by large widgets.

Figure 11.8 shows the default behavior of flex grid sizer when the window size is adjusted-cell size does not change.

Fig 11.8

Example 11.6 showsCode

In Example 11.6, create a flex grid sizer.

Import WX
From blockwindow import blockwindow

Labels = "One two three four five six seven eight nine". Split ()

Class testframe (wx. Frame ):
Def _ init _ (Self ):
Wx. Frame. _ init _ (self, none,-1, "flexgridsizer ")
Sizer = wx. flexgridsizer (rows = 3, cols = 3, hgap = 5, vgap = 5)
For label in labels:
BW = blockwindow (self, label = label)
Sizer. Add (BW, 0, 0)
Center = self. findwindowbyname ("five ")
Center. setminsize (150,50 ))
Self. setsizer (Sizer)
Self. Fit ()

APP = wx. pysimpleapp ()
Testframe (). Show ()
App. mainloop ()

One flex grid sizer is an instance of Wx. flexgridsizer. The wx. flexgridsizer class is a subclass of Wx. gridsizer, so the property method of Wx. gridsizer is still valid. The constructor of Wx. flexgridsizer is the same as its parent class:

Wx. flexgridsizer (rows, cols, vgap, hgap)

To expand a row or column when the sizer extends, You need to explicitly tell the sizer that the row or column is extensible using the appropriate method:

Addgrowablecol (idx, proportion = 0)
Addgrowablerow (idx, proportion = 0)

When sizer scales horizontally, the default behavior about the new width is assigned to each extensible column. Similarly, a vertical dimension adjustment is equally allocated to each extensible row. You need to use the proportion parameter to change the default behavior and make different rows and columns have non-existent expansion ratios. If the proportion parameter is used, the new space related to the parameter is allocated to the corresponding row or column. For example, if you have two rows with adjustable sizes and their proportion values are 2 and 1, the first row gets 2/3 of the new space, and the second row gets 1/3. Figure 11.9 shows the flex grid sizer that uses the proportional (ratio column) space. Here, the ratio of the middle row and column is 2 and 5, and the ratio of the rows and columns at both ends is 1.

Fig 11.9

As you can see, when all cells increase, the increase of rows and columns in the middle is twice that of the two ends. Widgets are not resized to fill in their cells, though they can be achieved by using wx. Expand when they are added to sizer. Example 11.7 shows the code that generates figure 11.9.

Example 11.7

Import WX
From blockwindow import blockwindow

Labels = "One two three four five six seven eight nine". Split ()

Class testframe (wx. Frame ):
Def _ init _ (Self ):
Wx. Frame. _ init _ (self, none,-1, "Resizing flex grid sizer ")
Sizer = wx. flexgridsizer (rows = 3, cols = 3, hgap = 5, vgap = 5)
For label in labels:
BW = blockwindow (self, label = label)
Sizer. Add (BW, 0, 0)
Center = self. findwindowbyname ("five ")
Center. setminsize (150,50 ))
Sizer. addgrowablecol (0, 1)
Sizer. addgrowablecol (1, 2)
Sizer. addgrowablecol (2, 1)
Sizer. addgrowablerow (0, 1)
Sizer. addgrowablerow (1, 5)
Sizer. addgrowablerow (2, 1)
Self. setsizer (Sizer)
Self. Fit ()

APP = wx. pysimpleapp ()
Testframe (). Show ()
App. mainloop ()

If you use a proportional size for an extensible row or column, you need to specify a proportional size for all extensible rows or columns in this direction, otherwise, you will get a bad result.

In Flex grid sizer, there is another mechanism used to control the growth of the window parts (do not execute the settings of the previous addgrowable * method ). By default, the scale size applies to the two directions of the Flex grid. However, you can use the setflexibledirection (Direction) method to specify that the scale size should be adjusted proportionally in only one direction, the value of the direction parameter can be wx. horizontal, wx. vertical, or WX. both (default ). Then you can use the setnonflexiblegrowmode (mode) method to specify the behavior in another direction. For example, if you call the setflexibledirection (wx. Horizontal) method, the column behavior follows addgrowablecol (), and then call setnonflexiblegrowmode () to define the row behavior. Table 11.3 lists the valid values of the mode parameter.

Table 11.3

Wx. flex_growmode_all: Flex grid adjusts the size of all cells in the same direction without setflexibledirection. This will overwrite any behavior set using the addgrowable * method-all cells will be resized regardless of their proportions or whether they are specified as extensible (increasing.

Wx. flex_growmode_none: The cell size does not change in the direction where setflexibledirection * is not used, regardless of whether they are specified as incrementing.

Wx. flex_growmode_specified: In the direction where setflexibledirection * is not used, only the cells that can be increased will grow. However, sizer ignores any proportion information and grows those cells. This is a default action.

the setflexibledirection and setnonflexiblegrowmode methods discussed in the preceding section both have corresponding methods: getflexibledirection () and getnonflexiblegrowmode (), which return the integer mark. The preceding table emphasizes that any settings specified using these methods will replace those created through addgrowablecol () and addgrowablerow.

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.