wxpython notes ·: wxpython interface layout (a)

 

wxpython five layout:

  • Grid: a very basic grid layout. When you want to place the window member is the same size and neatly into a regular grid it is used.
  • Flex grid: for grid sizer made some changes slightly, when the window member having different sizes, can have a better result.
  • Grid bag: grid sizer Series the most flexible members. The grid member such that the window can be placed more freely.
  • Box: layout in a horizontal or vertical line of the window member. When the size is changed, it is flexible in the behavior of the control widget. Commonly used for nested pattern. It can be used for almost any type of layout.
  • Static box: a standard box sizer. With a title and Link.

 

Here we will introduce Grid, Box, Static box three layout mode.

grid Sizer

Grid: a very basic grid layout. When you want to place the window member is the same size and neatly into a regular grid it is used.

A grid sizer is an instance of class wx.GridSizer.

Constructor explicitly provided four properties:

wx.GridSizer(rows, columns, vgap, hgap)

rows: create how many rows

columns: create how many columns

vgap: the amount of spacing between pixels in two adjacent columns

hgap: the amount of spacing between pixels in two adjacent rows

grid sizer size of each space is the same, even if different sizes of each widget, grid sizer most suitable for the case where all the child windows of the same size.

wx.GridSizer class common methods:

  • Add () to add the next available control slot grid
  • AddMany () to add each item in the list control
  • SetRows () number of rows in the sizer
  • The GetRows () to retrieve the number of lines of the sizer
  • SetCols () provided sizer Number of columns
  • () GetCols size retrieve the column number
  • SetVgap () a vertical gap (pixels) disposed between unit
  • GetVgap () vgap cells between the return value
  • SetHgap () horizontal gap (pixels) disposed between unit
  • () GetHgap value between hgap returns the specified cell

Examples

import wx
class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(300, 200))
        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        p = wx.Panel(self)
        gs = wx.GridSizer(4, 4, 5, 5)
        for i in range(1, 17):
            btn = "Btn" + str(i)
            gs.Add(wx.Button(p, label=btn), 0, wx.EXPAND)
            p.SetSizer (GS) # associating it with the frame GridSizer 

App = the wx.App ()
Example(None, title='Grid Demo - www.yiibai.com')
app.MainLoop ()

 

 

Box Sizer

 box: a box sizer is a horizontal row or vertical column, in which a window member disposed from left to right or top to bottom in a line.

wx.BoxSizer box sizer instance of the class, the subclass is wx.BoxSizer wx.Sizer with respect wx.Sizer, wx.BoxSizer almost no new method.

wx.BoxSizer a constructor argument:

wx.BoxSizer(orient)

Representative parameters orient the direction of the sizer, its value may be wx.VERTICAL or wx.HORIZONTAL.

Create vertical and horizontal box:

Box1 = wx.BoxSizer(wxHORIZONTAL)
Box2 = wx.BoxSizer(wxVERTICAL)

box sizer is a sizer wxPython provided the easiest and most flexible sizer.

Common method wx.BoxSizer class:

  • SetOrientation () provided or oriented wxHORIZONTAL wxVERTICAL
  • AddSpacer () adding a non-stretchable space
  • AddStretchSpacer () adds the telescopic space, in order to adjust the size of the control window affects proportional to the size
  • Clear () to remove the child from the sizer controls
  • Detach () from sizer delete controls do not destroy
  • Insert () Insert a child control at the specified position
  • Remove () Removes a child control from the sizer and destruction

Examples

import wx

class Mywin(wx.Frame):
    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title,size=(600, 400))
        Panel1 = wx.Panel (Self)
         # vertical box 
        VBox = wx.BoxSizer (wx.VERTICAL)
         # hbox 
        nmbox = wx.BoxSizer (wx.HORIZONTAL)
         # Create two static text 
        fn = wx.StaticText (panel1, -1 , label = " movie name: " )
        LN = wx.StaticText (Panel1, -1, label = " Select sources: " )
         # Create two input boxes 
        self.nm1 = wx.TextCtrl (Panel1, -1, size = (200,20), style = WX .ALIGN_LEFT)
         # add the top three in a horizontal box 
        nmbox.Add (the Fn, 0, wx.ALL | wx.EXPAND, 5 )
        nmbox.Add(self.nm1, 0, wx.ALL|wx.EXPAND, 5)
        nmbox.Add (LN, 0, wx.ALL | wx.EXPAND, 5 )
         # create a drop-down box 
        self.languages = [ ' movie Paradise ' , ' 80S Movie Network ' , ' 1905 Movie Network ' , ' BT heaven ' , ' Long tribe ' , ' film in Hong Kong ' , ' everybody subtitles group ' ]
        self.choice =self.languages[0]
        self.combo = wx.ComboBox (Panel1, choices = self.languages, value = self.languages [0])
         # added drop-down box hbox 
        nmbox.Add (self.combo, 1, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5 )


        # Create button 
        self.find_Button = wx.Button (Panel1, label = " query " )
        self.Bind(wx.EVT_BUTTON, self.Onclick, self.find_Button)
        # Add Query button on the horizontal box 
        nmbox.AddSpacer (20 )
        nmbox.Add(self.find_Button, 0, wx.ALIGN_CENTER_HORIZONTAL)
        # Add to the vertical box hbox 
        vbox.Add (nmbox, 0, wx.ALL | wx.CENTER,. 5 )

        MULTILABEL = wx.StaticText (Panel1, -1, " Results: " )
        vbox.Add(multiLabel, 0, wx.ALL | wx.EXPAND, 5)

        # Create a text field 
        self.multiText = wx.TextCtrl (Panel1, -1, size = (200 is, 200 is), style = wx.TE_MULTILINE)   # Create a text control 
        self.multiText.SetInsertionPoint (0)   # set the insertion point 
        #   in add text box vertical field 
        vbox.Add (self.multiText,. 1, wx.ALL | wx.EXPAND,. 5 )

        panel1.SetSizer(vbox)
        self.Show()

    def Onclick(self,event):
       print(self.choice)
       self.multiText.write(self.choice+"\n")
app = wx.App()
Mywin (None, ' fast search ' )
app.MainLoop ()

Highlights that the box of the add method:

Add () method (inherited from wxSizer) sizer attach it to the next row / column.
Box.Add(size, proportion, flag, border) 
size: the window needs to be added.
border: parameter is an integer, the pixel spacing is provided between the control space.
proportion: in response to a control parameter for controlling the size of the container to change its size. 

flag: the combination of parameters determines the appearance of controls sizer.
Align mark
wx.ALIGN_TOP
wx.ALIGN_BOTTO
Mwx.ALIGN_LEFT
wx.ALIGN_RIGHT
wx.ALIGN_CENTER_VERTICAL
wx.ALIGN_CENTER_HORIZONTAL

Boundary mark
wx.TOP
wx.BOTTOM
wx.LEFT
wx.RIGHT
wx.ALL

Behavioral signs          
wx.EXPAND project will expand to fill the space given to it (wx.GROW is the same)
wx.SHAPED and EXPAND similar, but kept the project aspect ratio
wx.FIXED_MINSIZE not allow the project to become smaller than the minimum size of its initial
wx.RESERVE_SPACE_EVEN_IF_ HIDDEN allowed measurer (Sizer) recovering project space, when it is hidden
flag parameters
Box sizer layout algorithm to treat the main direction of the sizer is (has been defined when constructing its direction parameter) and the secondary direction are different. In particular, proportion parameter applies only when the main-sizer
When the direction of stretching, while wx.EXPAND mark applies only to dimensional change when the sizer in the secondary direction. In other words, when a vertical box sizer is drawn perpendicularly, transmitted to each of the Add () method call
Proportion parameter determines how each project will stretch vertically. In addition to affecting the level of growth sizer and its projects, the proportion parameters affect the level of the box sizer in the same way. on the other hand,
Growth in sub-direction by wx.EXPAND markers used to control the project, so the project in a vertical box sizer will grow only in the horizontal direction, if they set wx.EXPAND mark it, 
otherwise these items maintain their minimum or optimum size.

 

Static box Sizer

Static box: a static box merged box sizer and the static (static box), static frame provides a nice border around the label and the text of the sizer.

static box sizer wx.StaticBoxSizer the instance of the class, wx.StaticBoxSizer is wx.BoxSizer subclass.

It constructor parameter is the static requirements and directions:

wx.StaticBoxSizer(box, orient)

orient: wx.BoxSizer same meaning as the original.

box: is a wx.StaticBox.

wx.StaticBox class has a standard wxPython constructor for control, but many of the parameters have default values ​​can be ignored.

wx.StaticBox(parent, id, label, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name=”staticBox”)

Using a static box sizer, you do not need to set pos, size, style, or a parameter name, location and size as the management by the sizer, and useless for wx.StaticBox individual style. This configuration makes
manufacturing easier:

box = wx.StaticBox(self.panel, -1, boxlabel)

 

Examples

step:

  • Creating a wx.StaticBox object.
  • Create a wx.StaticBoxSizer, will wx.StaticBox object as its argument.
import wx


class Mywin(wx.Frame):
    def __init__(self, parent, title):
        Super (mywin, Self). the __init__ (parent, title = title)
         # Create whiteboard 
        Panel = wx.Panel (Self)
         # create vertical and horizontal box box 
        VBox = wx.BoxSizer (wx.VERTICAL)
        nmbox = wx.BoxSizer(wx.HORIZONTAL)

        # Create a wx.StaticBox object. 
        # Declare a wx.StaticBoxSizer and wx.StaticBox objects created as its argument. 
        = wx.StaticBox nm (Panel, -1, ' the Name: ' )
        nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL)

        # Construction of static text input box 
        Fn = wx.StaticText (Panel, -1, " First the Name " )
        nm1 = wx.TextCtrl(panel, -1, style=wx.ALIGN_LEFT)
        ln = wx.StaticText(panel, -1, "Last Name")
        Nm2 of = wx.TextCtrl (Panel, -1, style = wx.ALIGN_LEFT)
         # add four horizontal upper box 
        nmbox.Add (Fn, 0, wx.ALL | wx.CENTER,. 5 )
        nmbox.Add(nm1, 0, wx.ALL | wx.CENTER, 5)
        nmbox.Add(ln, 0, wx.ALL | wx.CENTER, 5)
        nmbox.Add (Nm2 of, 0, wx.ALL | wx.CENTER,. 5 )
         # add hbox in StaticBoxSizer 
        nmSizer.Add (nmbox, 0, wx.ALL | wx.CENTER, 10 )
        
        # Add to the vertical box StaticBoxSizer cassette 
        vbox.Add (nmSizer, 0, wx.ALL | wx.CENTER,. 5 )
        panel.SetSizer(vbox)
        self.Centre()
        panel.Fit()
        self.Show()
        # Relationship: 
        # vertical box box box Add StaticBoxSizer 
        # StaticBoxSizer Box hbox Add   
App = the wx.App ()
Mywin(None, 'StaticBoxSizer')
app.MainLoop ()

From the results: a static closed box box box with a new layer border, and text.

 

 

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12146472.html