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

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

You might think that a wxTextCtrl is a simple control, but if you think that, you are very wrong. The wxTextCtrl contains nearly fifty methods. Note that I cannot cover that amount of methods in this article while keeping your interest, so don't expect me to. Instead, I'll just cover the more interesting ones.

First, let's build an application for creating a wxTextCtrl. We'll use our knowledge from the last article to center the control, and we'll also throw in a label:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

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

      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 )

      self.vertical.Add ( self.text, 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()

When I was learning HTML, one of the things I found interesting was aligning text in a text box. Text can also be aligned in a wxTextCtrl using wxTE_LEFT, wxTE_CENTRE and wxTE_RIGHT:

from wxPython.wx import *

class OurFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )

      # Create a left-aligned text box ( default behavior )

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

      # Create a centered text box

      self.center = wxTextCtrl ( self.panel, -1, style = wxTE_CENTRE )

      

      # Create a right-aligned text box

      self.right = wxTextCtrl ( self.panel, -1, style = wxTE_RIGHT )

      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.center, 0, wxALIGN_CENTER )

      self.vertical.Add ( self.right, 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()



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