Home arrow Python arrow A Close Look at wxPython Controls

A Close Look at wxPython Controls

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

wxChoice

Here is how a basic wxChoice control is put into an application:

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

Note that self is the parent, and -1 is the same as wxID_ANY. The list we provide at the end is, of course, the list of options availible to the user. Let's put our control in an application so that we can work with it:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

Currently, nothing is selected on default. However, suppose we want the second option to be selected on default. We can do this with wxChoice's SetSelection method. It accepts one argument, which is the position it should start at. “Choice A” is 0, and it increments from there. Let's force “Choice B” to be selected:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      self.choice.SetSelection ( 1 )

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

What if, however, we don't know the position of the string we want to select? This is easily solved. Let's select “Choice C” without directly passing its position:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      positionC = self.choice.FindString ( 'Choice C' )

      self.choice.SetSelection ( positionC )

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

The GetString method is the opposite of FindString. It accepts a position and returns the string in that position:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      stringC = self.choice.GetString ( 2 )

      positionC = self.choice.FindString ( stringC )

      self.choice.SetSelection ( positionC )

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

We can manipulate the items in the list using a variety of methods:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      # Delete an item ( "Choice A" )

      self.choice.Delete ( 0 )

      # Delete everything

      self.choice.Clear()

      # Add a few choices back

      self.choice.Append ( 'Choice E' )

      self.choice.Append ( 'Choice F' )

      self.choice.Append ( 'Choice G' )

      # Replace a  choice ( "Choice F" )

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

      # Count the items

      itemNum = self.choice.GetCount()

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

Finally, we are left with the task of extracting what the user selected. This is extremely simple, fortunately:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

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

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

      self.sizer = wxBoxSizer ( wxVERTICAL )

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

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

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

   def click ( self, event ):

      # Display the position of the selection

      print self.choice.GetSelection()

      # Display the selection

      print self.choice.GetStringSelection()

application = wxPySimpleApp()

window = OurFrame()

application.MainLoop()

You can also use EVT_COMBOBOX, which calls the function passed to it when the user selects a new option.



 
 
>>> 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: