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

Password text boxes, read-only text boxes - 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

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
 

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: