Python
  Home arrow Python arrow Page 2 - A Close Look at wxPython Controls
IBM developerWorks
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

A Close Look at wxPython Controls
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-07-06

    Table of Contents:
  • A Close Look at wxPython Controls
  • wxComboBox and wxListBox
  • wxListBox is versatile
  • wxTextCtrl
  • Password text boxes, read-only text boxes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    A Close Look at wxPython Controls - wxComboBox and wxListBox


    (Page 2 of 5 )

    The wxComboBox and wxListBox controls are similar to wxChoice. The three controls share many methods. Let's take a quick look at the two controls and compare them with wxChoice:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

          # Create the wxChoice control

          self.choice = wxChoice ( self.panel, -1, choices = [ 'A', 'B', 'C', 'D' ] )

          # Create the wxComboBox control

          self.combo = wxComboBox ( self.panel, -1, '0', choices = [ '1', '2', '3', '4' ] )

          # Create the wxListBox control

          self.list = wxListBox ( self.panel, -1, choices = [ 'I', 'II', 'III', 'IV' ] )

          # Append an option to each control

          self.choice.Append ( 'E' )

          self.combo.Append ( '5' )

          self.list.Append ( 'V' )

          # Delete an option from each control

          self.choice.Delete ( 0 )

          self.combo.Delete ( 0 )

          self.list.Delete ( 0 )

          # Replace choices

          self.choice.SetString ( 1, 'X' )

          self.combo.SetString ( 1, 'X' )

          self.list.SetString ( 1, 'X' )

          # Count items

          self.choice.itemNum = self.choice.GetCount()

          self.combo.itemNum = self.combo.GetCount()

          self.list.itemNum = self.list.GetCount()

          # Add a button

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

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

          self.sizer = wxBoxSizer ( wxVERTICAL )

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

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

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

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

          self.panel.SetSizerAndFit ( self.sizer )

          self.Show ( True )

       def click ( self, event ):

          # Display the position of all selections

          print self.choice.GetSelection(), self.combo.GetSelection(), self.list.GetSelection()

          # Display the selection

          print self.choice.GetStringSelection(), self.combo.GetStringSelection(), self.list.GetStringSelection()

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    The wxComboBox and wxListBox controls, however, do contain additional methods that you may use. First, we will tackle wxComboBox. As you know, wxComboBox provides the user with both a list and a text box. If you played around with the last example a bit, you probably saw that the GetStringSelection method will not return values that the user has typed in. Since GetSelection will return -1, however, it is easy to work around this:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

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

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

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

          self.sizer = wxBoxSizer ( wxVERTICAL )

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

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

          self.panel.SetSizerAndFit ( self.sizer )

          self.Show ( True )

       def click ( self, event ):

          if self.combo.GetSelection() == -1:

             self.combo.userValue = self.combo.GetValue()

          else:

             self.combo.userValue = self.combo.GetStringSelection()

          print self.combo.userValue

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    More Python Articles
    More By Peyton McCullough


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

    IBM developerWorks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway