Home arrow Python arrow Page 3 - A Close Look at wxPython Controls

wxListBox is versatile - Python

Controls are, obviously, essential to your wxPython applications. They allow you to gather input from the user so that you can make decisions based on that input. Let's take a close look at a few controls availible to you and your applications. This article will cover wxChoice, wxComboBox, wxListBox and wxTextCtrl. It is the third one in a series about wxPython.

TABLE OF CONTENTS:
  1. A Close Look at wxPython Controls
  2. wxComboBox and wxListBox
  3. wxListBox is versatile
  4. wxTextCtrl
  5. Password text boxes, read-only text boxes
By: Peyton McCullough
Rating: starstarstarstarstar / 15
July 06, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The wxListBox control supports multiple selections. We must handle this differently as well:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'wxPython' )

      self.panel = wxPanel ( self, -1 )

      self.list = wxListBox ( self.panel, -1, choices = [ '1', '2', '3', '4' ], style = wxLB_MULTIPLE )

      self.button = wxButton ( self.panel, 100, 'Click Me...now.' )

      EVT_BUTTON ( self.panel, 100, self.click )

      self.sizer = wxBoxSizer ( wxVERTICAL )

      self.sizer.Add ( self.list, 0 )

      self.sizer.Add ( self.button, 0 )

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

   def click ( self, event ):

      selections = self.list.GetSelections()

      if len ( selections ) > 1:

         for selection in selections:

            print 'Multiple Selected:', self.list.GetString ( selection )

      else:

         print 'Single Selected:', self.list.GetString ( selections [ 0 ] )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

We can insert a whole list of items rather than one at a time, and we can also reset the list entirely, without using Clear:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'wxPython' )

      self.panel = wxPanel ( self, -1 )

      self.list = wxListBox ( self.panel, -1, choices = [ '1', '2', '3', '4' ] )

      # Reset it

      self.list.Set ( [ 'Choice 1', 'Choice 2', 'Choice 3', 'Choice 4' ] )

      # Add some more items

      self.list.InsertItems ( [ 'Choice 5', 'Choice 6', 'Choice 7', 'Choice 8', 'Choice 9', 'Choice 10' ], 4 )

      self.lilst.InsertItems ( [ 'Choice 0' ], 0 )

      self.sizer = wxBoxSizer ( wxVERTICAL )

      self.sizer.Add ( self.list, 0 )

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

Finally, let's look at some events:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'wxPython' )

      self.panel = wxPanel ( self, -1 )

      self.list = wxListBox ( self.panel, 100, choices = [ 'One', 'Two', 'Three', 'Four' ] )

      self.combo = wxComboBox ( self.panel, 200, choices = [ 'Red', 'Blue', 'Green', 'Pink' ] )

      EVT_LISTBOX ( self.panel, 100, self.click)

      EVT_LISTBOX_DCLICK ( self.panel, 100, self.listDouble )

      EVT_COMBOBOX ( self.panel, 200, self.click )

      EVT_TEXT_ENTER ( self.panel, 200, self.comboEnter )

      EVT_TEXT ( self.panel, 200, self.comboText )

      self.sizer = wxBoxSizer ( wxVERTICAL )

      self.sizer.Add ( self.list, 0 )

      self.sizer.Add ( self.combo, 0 )

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

   def click ( self, event ):

      print 'Single click.'

   def listDouble ( self, event ):

      print 'Double click.'

   def comboEnter ( self, event ):

      print 'You pressed enter in the wxComboBox.'

   def comboText ( self, event ):

      print 'You changed the text in the wxComboBox.'

application = wxPySimpleApp()

window = OurFrame()

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: