wxPython wxTextCtrl and Buttons - Get,Set Values,Clicks etc

| Page Views: 249

In this class we will look at how to use Labels, TextFields and Buttons in wxPython.

We render a simple input form in our wx Form. The user types data into the textfields, he then presses one button to get whatever was typed displayed in a wxPython messagebox.

He clicks another button to set the old values.

What is wx.Button?

A Button, generally, is a control that reacts to click events so as to initiate an action. Buttons are one if the most common User Interface elements.

Through buttons users can instruct an application to perform a pre-defined action just through a click event.

Obviously wxPython also include Buttons. These be placed on a dialog box or on a wx.Panel panel, or indeed on almost any other window.

If none of the alignment styles are specified, the label is centered both horizontally and vertically.

You can create a basic wx.Button with text only as its caption.

A two-state button is also available, which is named as wx.ToggleButton. We can use this toggle between different states.

Another type of button, wx.BitmapButton displays a bitmap (image) as icon on its face.

If the button has both a label and a bitmap, the alignment styles above specify the location of the rectangle combining both the label and the bitmap and the bitmap position set with wx.Button.SetBitmapPosition defines the relative position of the bitmap with respect to the label

Note that since version 2.9.1 wx.Buttons support showing both text and images and you are not required to use wx.BitmapButton for that.

We are Building a Vibrant YouTube Community

We have a fast rising YouTube Channel of friends. So far we've accumulated more than 2.6 million agreggate views and more than 10,000 subscribeers. Here's the Channel: ProgrammingWizards TV.

Please go ahead subscribe(free obviously) as well. If you have a question or a comment you can post there instead of in this site.People are suggesting us tutorials to do there so you can too.

Here's this tutorial in Video Format.

wx.Button Events

The wx.Button provides us with the following event which we can handle as we will see in our example:

  • EVT_BUTTON: Process a wxEVT_BUTTON event, when the button is clicked.

let's start.

API Definition

class wx.Button(AnyButton)

Here are the constructors for wx.Button:


Button(parent, id=ID_ANY, label="", pos=DefaultPosition,
       size=DefaultSize, style=0, validator=DefaultValidator,


Normally in applications you always need to input some data. You need to allow users to type some inputs into the application for example.

Therefore wxPython provides us with the wx.TextCtrl component which allows text to be edited and displayed.

wxTextCtrl is what is normally called TextField, TextBox or EditText in other common GUI toolkits.

wx.TextCtrl supports both single line and multiple lines.

Here are some styles which can be used:

  1. wx.TE_MULTILINE: The text control allows multiple lines. If this style is not specified, line break characters should not be used in the controls value.

  2. wx.TE_PASSWORD: The text will be echoed as asterisks.

  3. wx.TE_READONLY: The text will not be user-editable.

  4. wx.TE_RICH: Use rich text control under Win32, this allows to have more than 64KB of text in the control even under Win9x. This style is ignored under other platforms.

  5. wx.TE_AUTO_URL: Highlight the URLs and generate the TextUrlEvents when mouse events occur over them.

  6. wx.HSCROLL: With this a horizontal scrollbar will be created and used, so that text won’t be wrapped.

wx.TextCtrl Events

Here are some commonly used wx.TextCtrl events we can handle:

  1. EVT_TEXT: Respond to a wxEVT_TEXT event, generated when the text changes.

  2. EVT_TEXT_ENTER: Respond to a wxEVT_TEXT_ENTER event, generated when enter is pressed in a text control which must have wx.TE_PROCESS_ENTER style for this event to be generated.

wx.TextCtrl API

Here's the wx.TextCtrl API:

class wx.TextCtrl(Control, TextEntry)

Then these are the constructors you can use:


TextCtrl(parent, id=ID_ANY, value="", pos=DefaultPosition,
         size=DefaultSize, style=0, validator=DefaultValidator,

Let's now look at wx.TextCtrl and wx.Button example. We'll get and set text to our wx.TextCtrl and listen to wx.Button events.


import wx
import wx.xrc

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MrFrame(None)
        return True

class MrFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"wxPython TextFields and Buttons", pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )

        myGridSizer = wx.GridSizer( 0, 2, 0, 0 )

        self.nameLabel = wx.StaticText( self, wx.ID_ANY, u"Name", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.nameLabel.Wrap( -1 )
        myGridSizer.Add( self.nameLabel, 0, wx.ALL, 5 )

        self.nameTxt = wx.TextCtrl( self, wx.ID_ANY, "Casini", wx.DefaultPosition, wx.DefaultSize, 0 )
        myGridSizer.Add( self.nameTxt, 0, wx.ALL, 5 )

        self.propellantLabel = wx.StaticText( self, wx.ID_ANY, u"Propellant", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.propellantLabel.Wrap( -1 )
        myGridSizer.Add( self.propellantLabel, 0, wx.ALL, 5 )

        self.propellantTxt = wx.TextCtrl( self, wx.ID_ANY, "Nuclear Energy", wx.DefaultPosition, wx.DefaultSize, 0 )
        myGridSizer.Add( self.propellantTxt, 0, wx.ALL, 5 )

        self.getBtn = wx.Button( self, wx.ID_ANY, u"Get", wx.Point( -1,-1 ), wx.DefaultSize, 0 )
        myGridSizer.Add( self.getBtn, 0, wx.ALL, 5 )

        self.setBtn = wx.Button( self, wx.ID_ANY, u"Set", wx.DefaultPosition, wx.DefaultSize, 0 )
        myGridSizer.Add( self.setBtn, 0, wx.ALL, 5 )

        self.SetSizer( myGridSizer )

        self.Centre( wx.BOTH )

        # Connect Events
        self.getBtn.Bind( wx.EVT_BUTTON, self.getValues )
        self.setBtn.Bind( wx.EVT_BUTTON, self.setValues )

    def __del__( self ):

    # Our Virtual event handlers. They will handle our Button Click events
    def getValues( self, event ):
        wx.MessageBox(self.nameTxt.Value+" uses "+self.propellantTxt.Value,"Cosmic News")

    # Set values to textfields
    def setValues( self, event ):
        self.propellantTxt.Value="Nuclear Energy"

if __name__=="__main__":


Here's what we get when we run the project:

wxPython wxTextCtrl and Buttons

wxPython wxTextCtrl and Buttons

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post