Organization Methods Beyond Sizers

A well-designed application will have its widgets organized neatly so that a user can easily access them. To accomplish this, we can use sizers to organize widgets, making them easier to locate and use. However, there are more methods of organization beyond sizers. This article will introduce you to methods of organization that work in conjunction with sizers and such, so you can organize your widgets even more, further benefitting those who use your applications.

Switching Panels

A simple way to organize widgets is to give the user the option to change the controls on the screen completely with a single click. This can be done by switching panels when the user presses a button on the current panel. Each panel would have its own widgets. Going this route isn’t very hard. All that is necessary is to create several wxPanel subclasses with their own sets of widgets and then a wxFrame. Buttons for navigation would be placed on the panels. When a button is clicked, the panels would be quickly switched.

Let’s take a big leap and put out the whole thing:

from wxPython.wx import *

# Create the frame

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, ‘Organized Application’, size = ( 250, 250 ) )

      # Create an empty variable to house our panel

      self.panel = None

      self.Show ( True )

   # Create the methods that switch the panels

   def goPanel1 ( self, event ):

      self.panel = instance1

      instance1.Show ( True )

      instance2.Show ( False )

      instance3.Show ( False )

   def goPanel2 ( self, event ):

      self.panel = instance2

      instance1.Show ( False )

      instance2.Show ( True )

      instance3.Show ( False )

   def goPanel3 ( self, event ):

      self.panel = instance3

      instance1.Show ( False )

      instance2.Show ( False )

      instance3.Show ( True )

# Create one panel that uses absolute positioning

class Panel1 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Create some navigation buttons

      self.button2 = wxButton ( self, 2, ‘Panel 2′, pos = ( 5, 5 ) )

      self.button3 = wxButton ( self, 3, ‘Panel 3′, pos = ( 50, 35 ) )

      # Hook up the navigation events

      EVT_BUTTON ( self, 2, parent.goPanel2 )

      EVT_BUTTON ( self, 3, parent.goPanel3 )

      # Set the size of the panel to match the frame

      self.SetSize ( parent.GetClientSize() )

      # Do not make the panel visible at first

      self.Show ( False )

# Create one panel that uses a horizontal box sizer

class Panel2 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Create sizer

      self.sizer = wxBoxSizer ( wxHORIZONTAL )

      # Create some navigation buttons

      self.button1 = wxButton ( self, 1, ‘Panel 1′ )

      self.button3 = wxButton ( self, 3, ‘Panel 3′ )

      # Add the navigation buttons

      self.sizer.Add ( self.button1 )

      self.sizer.Add ( self.button3 )

      # Hook up the navigation events

      EVT_BUTTON ( self, 1, parent.goPanel1 )

      EVT_BUTTON ( self, 3, parent.goPanel3 )

      # Resize the sizer and add it

      self.sizer.SetMinSize ( parent.GetClientSize() )

      self.SetSizerAndFit ( self.sizer )

      # Do not make the panel visible at first

      self.Show ( False )

# Create one panel that uses a vertical box sizer

class Panel3 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Create sizer

      self.sizer = wxBoxSizer ( wxVERTICAL )

      # Create some navigation buttons

      self.button1 = wxButton ( self, 1, ‘Panel 1′ )

      self.button2 = wxButton ( self, 2, ‘Panel 2′ )

      # Add the navigation buttons

      self.sizer.Add ( self.button1 )

      self.sizer.Add ( self.button2 )

      # Hook up the navigation events

      EVT_BUTTON ( self, 1, parent.goPanel1 )

      EVT_BUTTON ( self, 2, parent.goPanel2 )

      # Resize the sizer and add it

      self.sizer.SetMinSize ( parent.GetClientSize() )

      self.SetSizerAndFit ( self.sizer )

      # Do not make the panel visible at first

      self.Show ( False )

application = wxPySimpleApp()

frame = Window()

# Create the panels

instance1 = Panel1 ( frame )

instance2 = Panel2 ( frame )

instance3 = Panel3 ( frame )

# Set the default panel

frame.goPanel1 ( None )

application.MainLoop()

First, we create our main frame class with an empty variable, panel. We then create three different panel classes, and create instances of everything. When the user pushes a button, the corresponding method is called, and the panel is switched. It’s nothing new or complex; it’s just a bit long. 

{mospagebreak title=Using Tabs for Navigation}

Using tabs for navigation is not unlike switching panels to navigate through sets of widgets. Each tab represents an individual panel, and the panel currently displayed is switched when a tab is pressed. The widget responsible for the arrangement of tabs is wxNotebook. Tabs are created by simply adding pages to the wxNotebook. Let’s create some panels and test out the widget:

from wxPython.wx import *

# Create the main window

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, ‘Frame title.’ )

      # Create a wxNotebook

      self.notebook = wxNotebook ( self, -1 )

      self.Show ( True )

# Create a panel

class Panel1 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Put a label in the panel

      self.label = wxStaticText ( self, -1, ‘This is the first panel.’, pos = ( 25, 25 ) )

# Create another panel

class Panel2 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few buttons

      self.button1 = wxButton ( self, -1, ‘This is a button.’, pos = ( 5, 5 ) )

      self.button2 = wxButton ( self, -1, ‘So is this.’, pos = ( 30, 30 ) )

      self.button3 = wxButton ( self, -1, ‘And this.’, pos = ( 25, 60 ) )

# Create yet another panel

class Panel3 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few text boxes

      self.text1 = wxTextCtrl ( self, -1, pos = ( 5, 5 ) )

      self.text2 = wxTextCtrl ( self, -1, pos = ( 5, 50 ), style = wxTE_MULTILINE )

      self.text3 = wxTextCtrl ( self, -1, pos = ( 30, 25 ), style = wxTE_PASSWORD )

application = wxPySimpleApp()

frame = Window()

# Create the panels

instance1 = Panel1 ( frame.notebook )

instance2 = Panel2 ( frame.notebook )

instance3 = Panel3 ( frame.notebook )

# Add the panels

frame.notebook.AddPage ( instance1, ‘Panel One’ )

frame.notebook.AddPage ( instance2, ‘Panel Two’ )

frame.notebook.AddPage ( instance3, ‘Panel Three’ )

application.MainLoop()

As you can see, it’s very simple to create a wxNotebook widget and populate it with panels. When the user clicks a tab, the corresponding panel is presented to the user.

{mospagebreak title=Using a List for Navigation}

Instead of using tabs to switch between panels, the option to use a list is available, and it works in almost the exact same way as tabs do (as far as scripting it goes). This is done by using the wxListbook widget. This method is useful if you have a large number of panels in your application. The wxNotebook widget and the wxListbook widget are so alike in creation that we can just recycle our wxNotebook script, replacing “Notebook” with “Listbook”:

from wxPython.wx import *

# Create the main window

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, ‘Frame title.’ )

      # Create a wxListbook

      self.listbook = wxListbook ( self, -1 )

      self.Show ( True )

# Create a panel

class Panel1 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Put a label in the panel

      self.label = wxStaticText ( self, -1, ‘This is the first panel.’, pos = ( 25, 25 ) )

# Create another panel

class Panel2 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few buttons

      self.button1 = wxButton ( self, -1, ‘This is a button.’, pos = ( 5, 5 ) )

      self.button2 = wxButton ( self, -1, ‘So is this.’, pos = ( 30, 30 ) )

      self.button3 = wxButton ( self, -1, ‘And this.’, pos = ( 25, 60 ) )

# Create yet another panel

class Panel3 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few text boxes

      self.text1 = wxTextCtrl ( self, -1, pos = ( 5, 5 ) )

      self.text2 = wxTextCtrl ( self, -1, pos = ( 5, 50 ), style = wxTE_MULTILINE )

      self.text3 = wxTextCtrl ( self, -1, pos = ( 30, 25 ), style = wxTE_PASSWORD )

application = wxPySimpleApp()

frame = Window()

# Create the panels

instance1 = Panel1 ( frame.listbook )

instance2 = Panel2 ( frame.listbook )

instance3 = Panel3 ( frame.listbook )

# Add the panels

frame.listbook.AddPage ( instance1, ‘Panel One’ )

frame.listbook.AddPage ( instance2, ‘Panel Two’ )

frame.listbook.AddPage ( instance3, ‘Panel Three’ )

application.MainLoop()

When the user clicks an item on the list, the corresponding panel is displayed on the screen. It is possible to put the list on any side of the main window by applying the styles wxLB_TOP, wxLB_BOTTOM, wxLB_RIGHT and wxLB_LEFT.

{mospagebreak title=Using a Drop-Down List for Navigation}

In case you don’t want to organize things with tabs or a list, you have the option of using a drop-down list to allow users to switch between virtual pages. The wxChoicebook widget makes this possible. As with the previous widget, the wxChoicebook widget is very similar to the wxNotebook widget in creation:

from wxPython.wx import *

# Create the main window

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, ‘Frame title.’ )

      # Create a wxChoicebook

      self.choicebook = wxChoicebook ( self, -1 )

      self.Show ( True )

# Create a panel

class Panel1 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Put a label in the panel

      self.label = wxStaticText ( self, -1, ‘This is the first panel.’, pos = ( 25, 25 ) )

# Create another panel

class Panel2 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few buttons

      self.button1 = wxButton ( self, -1, ‘This is a button.’, pos = ( 5, 5 ) )

      self.button2 = wxButton ( self, -1, ‘So is this.’, pos = ( 30, 30 ) )

      self.button3 = wxButton ( self, -1, ‘And this.’, pos = ( 25, 60 ) )

# Create yet another panel

class Panel3 ( wxPanel ):

   def __init__ ( self, parent ):

      wxPanel.__init__ ( self, parent, -1 )

      # Add a few text boxes

      self.text1 = wxTextCtrl ( self, -1, pos = ( 5, 5 ) )

      self.text2 = wxTextCtrl ( self, -1, pos = ( 5, 50 ), style = wxTE_MULTILINE )

      self.text3 = wxTextCtrl ( self, -1, pos = ( 30, 25 ), style = wxTE_PASSWORD )

application = wxPySimpleApp()

frame = Window()

# Create the panels

instance1 = Panel1 ( frame.choicebook )

instance2 = Panel2 ( frame.choicebook )

instance3 = Panel3 ( frame.choicebook )

# Add the panels

frame.choicebook.AddPage ( instance1, ‘Panel One’ )

frame.choicebook.AddPage ( instance2, ‘Panel Two’ )

frame.choicebook.AddPage ( instance3, ‘Panel Three’ )

application.MainLoop()

{mospagebreak title=Boxing in Controls}

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.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye