Now let's organize things. We'll take a look at sizers throughout this tutorial. Sizers are simply devices that let you organize widgets. The most basic type of sizer is wxBoxSizer. It lets you organize widgets horizontally and vertically. It is also possible to nest sizers, as I'll explain in a bit. First, let me show you the syntax. Here is how you would create a horizontal wxBoxSizer: wxBoxSizer ( wxHORIZONTAL ) Here's how you create a vertical wxBoxSizer: wxBoxSizer ( wxVERTICAL ) Of course, now we have to do something with our sizer. Let's dive right in to some code: from wxPython.wx import * class SizerFrame ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Sizers' ) self.panel = wxPanel ( self, -1 ) self.Show ( True ) application = wxPySimpleApp() All right. This should be bite-size, but let's tear it apart just in case there's anything you don't completely understand. We start our application as normal. We import everything we need and create a frame. We then add a panel and two controls. Notice that the panel is the parent of both of the controls. Next, we create a vertical wxBoxSizer. This organizes our conrols vertically. We then add our two controls and adjust the size of both the panel and the frame to match the sizes of the controls. Finally, we show our frame to the user. Creating a horizontal sizer is done the same way, with the exception of one variable: from wxPython.wx import * class SizerFrame ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Sizers' ) self.panel = wxPanel ( self, -1 ) self.Show ( True ) application = wxPySimpleApp() Neither looks especially pretty, especially the horizontal one. Let's modify the vertical one a bit more and align the button in the center. from wxPython.wx import * class SizerFrame ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Sizers' ) self.panel = wxPanel ( self, -1 ) self.Show ( True ) application = wxPySimpleApp() Notice how we pass three arguments to the Add method. The first, of course, is the control we want to add to the sizer. There's nothing new there. The second is how the control should look in proportion to other controls. For example, if you want one control to be twice as big as another, pass 2 and 1, respectively. We pass 0, since we don't want to alter the size of our button. The third and final argument can vary. Since we pass 0, we have the choice of aligning our control any way we want it. We choose to align it in the center. Here are other options: wxALIGN_LEFT
blog comments powered by Disqus |
|
|
|
|
|
|
|