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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

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





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