"Python" User graphical interface GUI Wxpython II

Source: Internet
Author: User

Wxpython-Layouts and events

This chapter mainly records the layout of Sizer and the use of events.

There is also a need to record: Sizer Add method plus blank, Sizer layout,sizer How to remove how effective

Layout

All the components described earlier, if they do not write their pos dead, they will overlap on the page, resulting in the inability to see. And Sizer is a great tool for optimizing layouts, which gives you the flexibility to manage the relative positions between components.

Sizer probably can be divided into Gridsizer (grid layout) and Boxsizer (linear layout). The usage of Sizer is summed up by the creation of Sizer to associate it to an object (usually a panel). Then add components for sizer using methods such as Add,insert. Calling fit and so on can let Sizer automatically calculate the size of the subassembly. The Sizer.detach method is used to remove the association of a part and Sizer. //This doubt, tried a variety of methods, including window refresh and so on, the effect is not good

  Gridsizer Grid Layout

There are four parameters in the construction method that determine the approximate style of the layout, respectively, the Rows,cols,vgap,hgap, respectively, the number of rows of the grid, the number of columns, two pixels between adjacent columns, and two pixels between adjacent rows.

When adding a component using the Add method, the default is to fill a line first and then start filling in the next line. Simple Gridsizer strictly abide by the meaning of grid layout, when the size of the window changes, the size of its grid will also change, resulting in the size or spacing of the components added in the grid will also change accordingly. Gridsizer is more suitable for tabular job が, and its improved version of Flexgridsizer can be more flexible in setting size and scaling relationships.

Grid layout example;

Testsizer = Gridsizer (rows=2,cols=2) Totalpanel= Panel (self,-1) Text1= Statictext (Totalpanel,-1,"Text1:") btn1= Button (Totalpanel,-1,"Button1") Text2= Statictext (Totalpanel,-1,"Text2:") btn2= Button (Totalpanel,-1,"Button") Testsizer.add (Text1,flag=align_center) Testsizer.add (Btn1,flag=align_center) Testsizer.add (Text2,flag=align_center) Testsizer.add (Btn2,flag=EXPAND) Totalpanel.setsizer (Testsizer)

Interface effect:

  

Flexgridsizer

The construction method and the Gridsizer are similar. But the difference between it and Gridsizer is that, by default, the FGS grid does not change as the size of the window changes. The so-called default situation is to call Addgrowablerow (x,m)/addgrowablecol (y,n) with the Sizer object. These two methods mean that the grid of a row or column will change with the size of the window. For example, Addgrowablerow (1) means that the second row changes as the length of the window changes, and a second parameter can be added to determine the weight of the line change. such as Addgrowablerow (0,1); Addgrowablerow means that both the first row and the second row are set to be vertically extended, and the window extends one unit, the first line extends One-third while the second row extends two-thirds units. The same addgrowablecol controls the extension and weighting of the column in the horizontal direction. (The concept of weights will be mentioned later in the Boxsizer)

Note: The extension described here is a grid-only extension, and the specific components within the grid can not be extended, it is necessary to see the Sizer at the add flag. For example, if flag is set to Align_center, the mesh extends but the component remains in the middle of the grid. Expand, the component always fills the entire grid, and so on.

  Conversely, if the Sizer.add component is set Flag=expand but the component is not set to growable, it is white blind, the window changes can not cause the mesh changes naturally can not cause the changes in the components.

  

Gridbagsizer

GS and FGS are added by default to fill the first line, fill the first line, and then go to the second row. And GBS can specify which row to add components to which column of the grid.

GBS is constructed without the rows and cols parameters, and the final sizer will be rendered in a few rows and columns depending on how much lower right corner of the component is added near the lower-right corner = =

GBS must indicate two parameters at add, pos= (x, y) and size= (m,n) meaning that the component fills the grid in the x+1 and y+1 columns and that the component will occupy the position of the M Row N column (a bit like the merged cell property in XLRD,XLWT)

Boxsizer

BS is a horizontal row or vertical column, which is the parameter that is used to construct the method is WX. Horizontal or wx.vertical to decide. For horizontal, its extension direction is longitudinal, for vertical nature is horizontal.

The Add method for BS has the proportion parameter. The proportion parameter is similar to the second parameter of the Addgrowablerow mentioned above, which indicates the extension weight that the window occupies in the extensible direction. It is usually possible to add components to several horizontal sizer, and then add these sizer as components to a vertical sizer to form a more comfortable looking page. Note that if you need to have a window extension when the component also extends the effect, you need to let those several horizontal Sizer also have Flag=expand, because Sizer and panel is also the same size, if not set EXPAND, The size of these sizer will not change.

About the add of BS, there is a little more commonly used, is flag=left| Right| TIP: Bottom| All,border=xx by one or more of the flag= options, and with the value of border, adjust the position of the add element to always keep a specified number of edges and around the border numeric pixels. Because it is often used to not wordy.

Event

Some interactions between human and component elements trigger events that are represented in WX in the following way:

Wx. Evt_button Pressing the button event

Common events include:

Evt_size is sent to the window when a window size changes due to user intervention or implementation by the program.
Evt_move is sent to a window when a window is moved because of user intervention or implementation by the program.
Evt_close sent to the framework when a frame is required to be closed. The event can be invoked unless the shutdown is mandatory. Veto (True) to cancel the shutdown.
Evt_paint is sent to the window whenever a part of the window needs to be redrawn.
Evt_char when the window has input focus, each time a non-modifiable (Shift key, and so on) key is generated.
Evt_idle This event is sent periodically when the system does not handle other events.
Evt_left_down the left mouse button press.
Evt_left_up the left mouse button to lift up.
Evt_left_dclick left mouse button double-click.
Evt_motion Mouse is moving.
The Evt_scroll scroll bar is manipulated. This event is actually a set of events that can be captured individually if needed.
The Evt_menu menu is selected .

You can also customize the event, but I don't think I'll be able to use it for the time being. can refer to http://blog.csdn.net/tingsking18/article/details/4033639

Bind method

Only events are not enough, you have to combine the component that caused the event and the event handling, and here is the BIND function

Bind belongs to Wx.evthandler, which is the parent of all the objects that can be displayed (most of the components, including the frame), so these components can call bind. Allows a component to bind multiple events, at which event can be written in the handler function. Skip (True) to identify an event handler that does not handle the change event and skips directly.

Bind is defined as bind (the binding event type, event handler, event source object), but it is usually possible to invoke bind such as btn.bind (binding event type, event handler) through a component. The event type is the upper one, and the event handler passes a function object. This function object requires only one parameter with no default value, event. This raises the question of what to do when I want to pass some extra arguments to the event handler function. For example:

 #   This is a healthy   Btn.    Bind (evt_button,test)  def   Test (event):  print   "  Hello   " #   But this will report the wrong number of arguments  btn. Bind (evt_buton,test)  def   Test (event,name ):  print   "  hello,%s   "% (name)  #   and in Bind, you cannot directly add parameters to test. 

This can actually be categorized as a more general problem, that is, what is the default value for a function object parameter (which is not given the default value of the parameter at the time of definition)?

  Workaround: Use lambda:

Btn. Bind (Evt_button,Lambda evt,name="Frank": Test (evt,name))def  Test (event,name):    print"hello,%s"% (name)

return a wrapped function object by using lambda as a wrapper for the function itself (it feels a bit like a decorator's meaning)

After adding events throughout WX, you can see that the relationships between components are complex. The proposed procedure is constructed in such a way that:

Each window is a class, and different windows are represented by different classes.

The components in the window are written in the constructor of the class, and the components that need to be evaluated and interact with other methods are set to "self. Components". The event-handling method that is bound to the component is written as a class method of the "self. Method". When a child window is required, the parent parameter is added to the construction method of the window, and then passed in when the child window is constructed.

"Python" User graphical interface GUI Wxpython II

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.