Python
  Home arrow Python arrow Page 5 - A Close Look at wxPython Controls
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

A Close Look at wxPython Controls
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    A Close Look at wxPython Controls - Password text boxes, read-only text boxes
    ( Page 5 of 5 )

    Another thing that interested me was creating text boxes for passwords, as well as text boxes that could not be modified. The wxTE_PASSWORD and wxTE_READONLY styles make this possible with the wxTextCtrl. We can also use the SetEditable method:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

          # Create a normal text box

          self.text = wxTextCtrl ( self.panel, -1 )

          # Create a password box

          self.passw = wxTextCtrl ( self.panel, -1, style = wxTE_PASSWORD )

          # Create a read-only box

          self.disabled = wxTextCtrl ( self.panel, -1, 'Cannot be modified.', style = wxTE_READONLY )

          # Create a read-only box

          self.disabled2 = wxTextCtrl ( self.panel, -1, 'Nor can this.' )

          self.disabled2.SetEditable ( False )

          self.vertical = wxBoxSizer ( wxVERTICAL )

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

          self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER )

          # Add everything

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

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

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

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

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

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

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

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

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

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    Multi-line text boxes are also very useful in applications. They can be created from a wxTextCtrl using the wxTE_MULTILINE style, and, optionally, the wxTE_DONTWRAP style:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

          # Create a text box

          self.text = wxTextCtrl ( self.panel, -1 )

          # Create a multi-line text box

          self.multi = wxTextCtrl ( self.panel, -1, style = wxTE_MULTILINE )

          # Create a multi-line text box that does not wrap lines

          self.multi2 = wxTextCtrl ( self.panel, -1, style = wxTE_MULTILINE | wxTE_DONTWRAP )

          self.vertical = wxBoxSizer ( wxVERTICAL )

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

          self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER )

          # Add everything

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

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

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

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

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

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

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

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

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    If we only want the user to be able to enter a certain amount of characters, we can do that with the SetMaxLength method:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

          # Create a text box

          self.text = wxTextCtrl ( self.panel, -1 )

          # Create a text box with a maximum length

          self.max = wxTextCtrl ( self.panel, -1 )

          self.max.SetMaxLength ( 10 )

          self.vertical = wxBoxSizer ( wxVERTICAL )

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

          self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER )

          # Add everything

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

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

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

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

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

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

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

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    If we want to trigger an event when the user presses the enter key on his or her keyboard, we can do so with the wxTE_PROCESS_ENTER style and the EVENT_TYPE_TEXT_ENTER_COMMAND event. When the user presses the enter key on our second text box, we'll write a few characters to the text box. When the user presses the enter key on our third text box, we'll set the value to something else. We'll also create two more text boxes. When the user presses the enter key while working with the third text box, its contents will be saved to a file, and the fourth text box will load the contents of that file:

    from wxPython.wx import *

    class OurFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )

          # Create a text box

          self.text = wxTextCtrl ( self.panel, -1 )

          # Create a text box that triggers an event

          self.enter = wxTextCtrl ( self.panel, 100, style = wxTE_PROCESS_ENTER )

          EVT_TEXT_ENTER ( self.panel, 100, self.handleEnter1 )

          # Create another text box that triggers an event

          self.enter2 = wxTextCtrl ( self.panel, 200, style = wxTE_PROCESS_ENTER )

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

          # Create another text box that triggers an event

          self.save = wxTextCtrl ( self.panel, 300, style = wxTE_PROCESS_ENTER )

          EVT_TEXT_ENTER ( self.panel, 300, self.handleEnter3 )

          # Create another text box that triggers an event

          self.load = wxTextCtrl ( self.panel, -1, style = wxTE_PROCESS_ENTER )

          self.vertical = wxBoxSizer ( wxVERTICAL )

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

          self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER )

          # Add everything

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

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

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

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

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

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

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

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

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

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

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

       def handleEnter1 ( self, event ):

          self.enter.AppendText ( '100%' )

       def handleEnter2 ( self, event ):

          self.enter2.SetValue ( '0%' )

       def handleEnter3 ( self, event ):

          self.save.SaveFile ( 'textctrl.txt' )

          self.load.LoadFile ( 'textctrl.txt' )

    application = wxPySimpleApp()

    window = OurFrame()

    application.MainLoop()

    Conclusion

    Now we know a bit more about the wxChoice, wxComboBox, wxListBox and wxTextCtrl controls, giving us more methods for gathering input from users, which is necessary in any application. We also know that although some wxPython controls seems simple on the surface, such as wxTextCtrl, they can be amazingly complex underneath, containing dozens of methods for us to use.



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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek