Home arrow Python arrow Page 2 - Checkboxes and Radio Buttons in wxPython

wxCheckListBox - Python

In this article, we will focus on two basic controls and a few relatives: checkboxes and radio buttons. Both controls give the user a variety of options and yet him or her choose anywhere from one to all of them.

TABLE OF CONTENTS:
  1. Checkboxes and Radio Buttons in wxPython
  2. wxCheckListBox
  3. wxRadioButton
  4. wxRadioBox
By: Peyton McCullough
Rating: starstarstarstarstar / 33
August 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The wxCheckListBox is a combination of wxCheckBox and wxListBox. It is basically a list of checkboxes that users can click. It's useful if you have a large amount of checkboxes. Let's create an application that shows off wxCheckListBox's features:

from wxPython.wx import *

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Dinner', size = ( 300, 300 ) )

      # Create a status bar

      self.CreateStatusBar()

      # Create a panel

      self.panel = wxPanel ( self, -1 )

      # Create a label

      self.label = wxStaticText ( self.panel, -1, 'Configure your dinner:' )

      # Define a list of items to place in the wxCheckListBox

      self.dinner = [ 'Ravioli', 'Apple Pie', 'Dr. Pepper', 'Soup', 'Salad', 'Ham Sandwich', 'Pizza', \

                      'Macaroni and Cheese', 'Carrot Cake', 'Spinach', 'Potato Salad ( Southern Style )' ]

      # Create a wxCheckListBox

      self.box = wxCheckListBox ( self.panel, 100, size = ( 150, 100 ), choices = self.dinner, style = wxLB_HSCROLL )

      # Hook up an event handling method

      EVT_CHECKLISTBOX ( self.panel, 100, self.clickHandler )

      # Create a button

      self.button = wxButton ( self.panel, 200, 'Prepare' )

      # Hook up an event handling method

      EVT_BUTTON ( self.panel, 200, self.buttonHandler )

      # Create two sizers to make everything pretty

      # The usual

      self.vertical = wxBoxSizer ( wxVERTICAL )

      self.vertical.Add ( ( 0, 0 ), 1 )

      self.vertical.Add ( self.label, 0, wxALIGN_CENTER )

      self.vertical.Add ( ( 5, 5 ), 0 )

      self.vertical.Add ( self.box, 0, wxALIGN_CENTER )

      self.vertical.Add ( ( 5, 5 ), 0 )

      self.vertical.Add ( self.button, 0, wxALIGN_CENTER )

      self.vertical.Add ( ( 0, 0 ), 1 )

      self.horizontal = wxBoxSizer ( wxHORIZONTAL )

      self.horizontal.Add ( ( 0, 0 ), 1 )

      self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER )

      self.horizontal.Add ( ( 0, 0 ), 1 )

      # Attach the sizer

      self.panel.SetSizerAndFit ( self.horizontal )

      self.Show ( True )

   # This method is called when an item is clicked

   def clickHandler ( self, event ):

      if self.box.IsChecked ( event.GetSelection() ):

         message = ' will be prepared.'

      else:

         message = ' will not be prepared.'

      # Update the status bar of the window

      self.SetStatusText ( self.box.GetString ( event.GetSelection() ) + message )

   # This method will be called when the button is clicked

   def buttonHandler ( self, event ):

      message = 'The following items have been prepared:'

      # Check to see what has been selected and what has not

      x = 0

      for choice in self.dinner:

         if self.box.IsChecked ( x ):

            message = message + '\n' + choice

         x = x + 1

      # Display a dialog

      dialog = wxMessageDialog ( self, message, 'Your dinner is ready.', style = wxOK )

      dialog.ShowModal()

      dialog.Destroy()

application = wxPySimpleApp()

Window()

application.MainLoop()



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: