Organization Methods Beyond Sizers - Boxing in Controls
(Page 5 of 5 )
Sometimes, it is helpful to group related controls together in a box. This can be done with the wxStaticBox widget very easily:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'Static Box', size = ( 300, 300 ) )
# Create a panel to house everything
self.panel = wxPanel ( self, -1 )
# Create the wxStaticBox
self.box = wxStaticBox ( self.panel, -1, 'Just a Box', size = ( 200, 200 ), pos = ( 5, 5 ) )
# Create a button
self.button = wxButton ( self.panel, -1, 'Just a button', pos = ( 25, 25 ) )
self.Show ( True )
application = wxPySimpleApp()
Window()
application.MainLoop()
One thing to note here is that the wxStaticBox is not the parent of our wxButton. If you set it as the parent of anything, your application will crash, and that's not a key ingredient for a healthy application.
If we want to organize controls in a wxStaticBox, we can use wxStaticBoxSizer, which works like wxBoxSizer. This can be hooked up to our wxStaticBox. We'll add another button to our box, though, and since this method is not too friendly with absolute positioning, we'll use wxBoxSizer. We'll create two of these to center everything:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'Static Box', size = ( 300, 300 ) )
# Create a panel to house everything
self.panel = wxPanel ( self, -1 )
# Create vertical box sizer
self.sizer = wxBoxSizer ( wxVERTICAL )
# Create a horizontal box sizer -- we're going to center everything
self.horizontal = wxBoxSizer ( wxHORIZONTAL )
# Create the wxStaticBox
self.box = wxStaticBox ( self.panel, -1, 'Just a Box' )
# Create our wxStaticBoxSizer
self.boxSizer = wxStaticBoxSizer ( self.box, wxHORIZONTAL )
# Create some buttons
self.button1 = wxButton ( self.panel, -1, 'Just a button' )
self.button2 = wxButton ( self.panel, -1, 'Another.' )
# Add the buttons to the sizer
self.boxSizer.Add ( self.button1 )
self.boxSizer.Add ( self.button2 )
# Configure the box sizers
self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )
self.horizontal.Add ( self.boxSizer )
self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )
self.sizer.Add ( ( 0, 0 ), 1, wxEXPAND )
self.sizer.Add ( self.horizontal, 0, wxALIGN_CENTER )
self.sizer.Add ( ( 0, 0 ), 1, wxEXPAND )
# Attach everything
self.panel.SetSizerAndFit ( self.sizer )
self.Show ( True )
application = wxPySimpleApp()
Window()
application.MainLoop()
Conclusion
This article has shown you that, beyond using simple sizers, there are additional ways to organize the controls in your application, giving the user more control over what he or she sees, which makes him or her appreciate your application more. Organization can be done in a variety of ways, including the ones that this article has outlined: switching panels, using tabs, using a list, using a drop-down list and using a wxStaticBox to organize closely related widgets in your application. Ease-of-use must be considered in any application, and this article has given you some tools to use in the process.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |