How to clear a Text-Entry field.

Hi there, I am trying to write a simple GUI application. I have a few windows and buttons. I would be grateful if someone could offer a snippet of code to help me clear my text entry field. Basically i have given my code below for you to use, i am trying to add a mehod to the 'CLEAR' button, which when selected will clear any text written in the Question entry wxTextCtrl box just above.

Thank you very kindly for any help

Here is the code:

···

#----------------------------------------------------------------------------
# Name: GUI.py
# Purpose: A Front-End wxPython (GUI) program, to interact with user input.
#
# Author: Mohammed Ahtesham Ilyas
#
# Created: 11/12/2003
# Student_ID: 0114455/1
# Copyright: (c) by Mohammed Ahtesham Ilyas (Kings College London)
# Licence: Python & wxPython open-source license
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------

## import all of the wxPython GUI package
from wxPython.wx import *
## import the calendar feature from the wxPython library
from wxPython.calendar import *
## import all extra utilities from the wxPython library
from wxPython.utils import *
import sys,os

#----------------------------------------------------------------------------

# I have used unique identifiers to associate each menu
# item. The convention is identifiers beginning
# with 'ID', followed by the menu the option belongs to, then
# the option name.

ID_FILE_OPEN = wxNewId()
ID_FILE_CLOSE = wxNewId()
ID_FILE_EXIT = wxNewId()
ID_HELP_WITH_SYSTEM = wxNewId()
ID_HELP_ABOUT = wxNewId()
ID_SUPPORT_ASSISTANCE = wxNewId()
ID_BUTTON = wxNewId()
ID_LOG = wxNewId()

## Create a new frame class, derived from the wxPython Frame.
class myFrame(wxFrame):
    def __init__(self, parent, id, title):
        # First, call the base class ' __init__ ' method to create the frame
        wxFrame.__init__(self, parent, id, title, style =
                wxDEFAULT_FRAME_STYLE)

#----------------------------------------------------------------------------
# Create a statusbar with some initial text
#----------------------------------------------------------------------------

        self.CreateStatusBar()
        self.SetStatusText('Find all train journey information right here!')

#----------------------------------------------------------------------------
# Layout the main Panels
#----------------------------------------------------------------------------
        # Add a top panel and some controls to display the size and position
        panelA = wxPanel (self, -1, size=(100,100), style = wxSUNKEN_BORDER)
        panelA.SetBackgroundColour(wxColour(red=240, green=180, blue=252))

        # Add a bottom panel and some controls to display the size and position
        panelB = wxPanel (self, -1, size=(100,100), style = wxSUNKEN_BORDER)
        panelB.SetBackgroundColour(wxNamedColour("MEDIUM ORCHID"))

#----------------------------------------------------------------------------
# Add Labels to Panels
#----------------------------------------------------------------------------

        # Add a label for the 'Welcome' title
        # then format the position, style and font on the top panelA
        str = "WELCOME"
        text = wxStaticText(panelA, -1, str, (295, 27))
        font = wxFont(55, wxROMAN, wxNORMAL, wxBOLD)
        text.SetFont(font)
        text.SetBackgroundColour(wxColour(red=240, green=180, blue=252))

        # Add a label for the clock
        # then format the position, style and font on botton panelB
        str = "TIME:"
        text = wxStaticText(panelB, -1, str, (130,20))
        font = wxFont(12, wxROMAN, wxNORMAL, wxBOLD)
        text.SetFont(font)
        text.SetBackgroundColour(wxNamedColour('MEDIUM ORCHID'))

        # Add a label for the input field
        # then format the position, style and font on panelB
        str = "Please Enter Your Question:"
        text = wxStaticText(self, -1, str, (355, 220))
        font = wxFont(17, wxROMAN, wxNORMAL, wxBOLD)
        text.SetFont(font)
        text.SetBackgroundColour(wxNamedColour('MEDIUM ORCHID'))

        # Add a label for the output area
        # then format the position, style and font on panelB
        str = "Response:"
        text = wxStaticText(panelB, -1, str, (320, 235))
        font = wxFont(15, wxROMAN, wxITALIC, wxBOLD)
        text.SetFont(font)
        text.SetBackgroundColour(wxNamedColour('MEDIUM ORCHID'))

        # Add a label for the calendar
        # then format the position, style and font on panelB
        str = "CALENDAR"
        text = wxStaticText(panelB, -1, str, (780,20))
        font = wxFont(12, wxROMAN, wxNORMAL, wxBOLD)
        text.SetFont(font)
        text.SetBackgroundColour(wxNamedColour('MEDIUM ORCHID'))

#----------------------------------------------------------------------------
# Additional panels
#----------------------------------------------------------------------------

        # Place a bar to underline the 'Welcome' title
        panel1= wxPanel(panelA, 100,wxPoint(295,105), wxSize(400,5),
                                 wxSIMPLE_BORDER)
        panel1.SetBackgroundColour(wxColour(red=110, green=120, blue=121))

        # Add a panel on which the clock is displayed
        panel= wxPanel(panelB, 100,wxPoint(85,50), wxSize(130,120),
                                 wxSIMPLE_BORDER)
        panel.SetBackgroundColour(wxNamedColour("MEDIUM ORCHID"))

#----------------------------------------------------------------------------
# Add Images to a panel
#----------------------------------------------------------------------------

        # Add a panel to the left of the 'Welcome' title for an image
        panel= wxPanel(panelA, 100,wxPoint(90,30), wxSize(120,70),
                                 wxSIMPLE_BORDER)
        panel.SetBackgroundColour(wxColour(red=240, green=180, blue=252))

# image = wxImage("C:\Documents and Settings\Ahtesham\Desktop.jpg",
# wxBITMAP_TYPE_JPEG).ConvertToBitmap()
#
# imageView = wxStaticBitmap(panel,-1, image, wxDefaultPosition, wxDefaultSize)

        # Add a panel to the right of the 'Welcome' title for an image
        panelx= wxPanel(panelA, 100,wxPoint(770,30), wxSize(120,70),
                                 wxSIMPLE_BORDER)
        panel.SetBackgroundColour(wxColour(red=240, green=180, blue=252))

#----------------------------------------------------------------------------
# Add buttons to a panel
#----------------------------------------------------------------------------

        # A 'Submit' button on the lower panelB
        submit = wxButton(panelB, -1, ' SUBMIT ', (407,200))
        submit.SetDefault()

        # A 'Clear' button on the lower panelB
        clear = wxButton(panelB, -1, ' Clear ', (498,200))
        clear.SetDefault()

        # Assign a method to clear the input field, on mousepress event.

#----------------------------------------------------------------------------
# Create the text fields
#----------------------------------------------------------------------------

        # single line text entry (input) field
        input = wxTextCtrl(panelB, 20, "<Enter your question here>",
                        wxPoint(340,140), wxSize(305,-1))
        #-----EVT_TEXT(self, 20, self.EvtText)
        #-----EVT_CHAR(self.editname, self.EvtChar use later!

        # A multiline TextCtrl for displaying system response/output
        response = wxTextCtrl(panelB,ID_LOG, "", wxPoint(315,260), wxSize(350,100),
                        wxTE_MULTILINE | wxTE_READONLY)
        response.SetInsertionPoint(200)

###Modify the input field here, to run the parser over it!

#----------------------------------------------------------------------------
# Add a calendar to the lower panelB
#----------------------------------------------------------------------------

        # create an instance of the calendar
        cal = wxCalendarCtrl(panelB, -1, wxDateTime_Now(), pos = (740,50),
                             style = wxCAL_SHOW_HOLIDAYS
                             > wxCAL_SUNDAY_FIRST
                             > wxCAL_SEQUENTIAL_MONTH_SELECTION
                             )

#----------------------------------------------------------------------------
# Create a menubar with several menus
#----------------------------------------------------------------------------

# create the 'File' menu, then add the 'Support' and 'Help' menu's.
# Notice that we use the identifiers created above to associate an ID
# to each menu item (we will use the ID again when
# we want to 'tell' the program what to do when the
# user selects a menu item).

        # create the 'File' menu
        file_menu = wxMenu()
        file_menu.Append(ID_FILE_OPEN, 'Open File',
                " Information about this program")
        file_menu.Append(ID_FILE_CLOSE, 'Close File',
                " To close a file")
        file_menu.AppendSeparator()
        file_menu.Append(ID_FILE_EXIT, 'Exit Program',
                " Select to EXIT this program")

        # create the 'Support' menu
        support_menu = wxMenu()
        support_menu.Append(ID_SUPPORT_ASSISTANCE, 'Assistance',
                " Select for how to seek assistance with the program")

        # create the 'Help' menu
        help_menu = wxMenu()
        help_menu.AppendSeparator()
        help_menu.Append(ID_HELP_WITH_SYSTEM, 'With this system...',
                        " Please do the following")

        # a submenu in the 'Help' menu
        submenu = wxMenu()
        submenu.Append(2031," Train Routes ( EXT: 2031 ) ")
        submenu.Append(2032," Delays and Cancellations ( EXT: 2032 ) ")
        submenu.Append(2033," Dyslexia and Spelling Difficulty ( EXT: 2033 ) ")
        help_menu.AppendMenu(203, "About", submenu)

        # we now need a menu bar to hold the menus created
        menu_bar = wxMenuBar()

        # Add our menus to the menu bar. The first argument is the
        # menu (created above), and the second is what we want to
        # appear on the menu bar.
        menu_bar.Append(file_menu, 'File')
        menu_bar.Append(support_menu, 'Support')
        menu_bar.Append(help_menu, 'Help')

        # set the menu bar (tells the system we're done)
        self.SetMenuBar(menu_bar)

#----------------------------------------------------------------------------
# Define sizers to layout panels (i.e. panelA and panelB)
#----------------------------------------------------------------------------
        box = wxBoxSizer(wxVERTICAL)
        box.Add(panelA, 1, wxEXPAND)
        box.Add(panelB, 4, wxEXPAND)

        self.SetAutoLayout(true)
        self.SetSizer(box)
        self.Layout()

#----------------------------------------------------------------------------
# Define methods and events for the menubar
#----------------------------------------------------------------------------

# Associate some events with methods of this class
        EVT_MENU(self, ID_FILE_EXIT, self.OnFileExit)
        EVT_MENU(self, ID_SUPPORT_ASSISTANCE, self.Enquiry)
        EVT_MENU(self, ID_HELP_WITH_SYSTEM, self.SystemHelp)

    def OnFileExit(self, evt):
        # This is executed when the user clicks the 'File' option
        # on the menu. We ask the user if they *really*
        # want to exit, then close everything down if they do.
            dlg = wxMessageDialog(self, 'Exit Program?', 'Are you sure you wish to exit!',
                              wxYES_NO | wxICON_QUESTION)

            if dlg.ShowModal() == wxID_YES:
                dlg.Destroy()
                self.Close(true)
            else:
                dlg.Destroy()

    def Enquiry(self, evt):
        dlg = wxMessageDialog(self, " In case of major system error(s), contact Ahtesham\n"
                                    " on the Emergency Number: (0208) 7882718 ",
                               " Information ",wxOK | wxICON_INFORMATION)

        dlg.ShowModal()

        if dlg.ShowModal() == wxOK:
            dlg.Destroy()
            self.Close(true)
        else:
            dlg.Destroy()

    def SystemHelp(self, evt):
        dlg = wxMessageDialog(self, " Please to do following:\n"
                              " 1. Seek assistance at the Information Desk. OR"
                              "\n"
                              " 2. Call the Helpline on ' (0800) 112233 '.",
                              "Information" ,wxOK | wxICON_INFORMATION)

        dlg.ShowModal()

        if dlg.ShowModal() == wxOK:
            dlg.Destroy()
            self.Close(true)
        else:
            dlg.Destroy()

#----------------------------------------------------------------------------
# Define methods and events for the calendar
#----------------------------------------------------------------------------

    # select a date
    def OnCalSelected(self):
        self.log.write('OnCalSelected: %s\n' % evt.GetDate())

    # change a month
    def OnChangeMonth(self, evt=None):
        cur_month = self.cal.GetDate().GetMonth() + 1 # convert wxDateTime 0-11 => 1-12
        for month, day in self.holidays:
            if month == cur_month:
                self.cal.SetHoliday(day)

        # Highlight the date selected
        EVT_CALENDAR(self, cal.GetId(), self.OnCalSelected)
        # Change the month on selection of the month arrows
        EVT_CALENDAR_MONTH(self, cal.GetId(), self.OnChangeMonth)
        # (these months don't move around)
        self.holidays = [(1,1), (10,31), (12,25) ]
        self.OnChangeMonth()

#----------------------------------------------------------------------------
# Every wxWindows application must have a class derived from wxApp
#----------------------------------------------------------------------------

class MyApp(wxApp):
    def OnInit(self):
        # Create an instance of our customized Frame class
        frame = myFrame(NULL, -1, '(RJIS) Rail Journey Information System')
        frame.Show(true)
        # Tell wxWindows that this is our main window
        self.SetTopWindow(frame)
        # Return a success flag
        return true

##########################################################################
## Run the application to test the code ##
##########################################################################

app=MyApp(1) # Create an instance of the application class
app.MainLoop() # Tell it to start processing events

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo

A. T. wrote:

Hi there, I am trying to write a simple GUI application. I have a few windows and buttons. I would be grateful if someone could offer a snippet of code to help me clear my text entry field. Basically i have given my code below for you to use, i am trying to add a mehod to the 'CLEAR' button, which when selected will clear any text written in the Question entry wxTextCtrl box just above.

Do this:

1. Create an 'ID_CLEARBUTTON = wxNewID()' when you define your IDs at the top.
2. Make the clear button an instance variable and associate with that ID: 'self.clearButton = wxButton(panelB, ID_CLEARBUTTON, ' Clear ', (498,200))
3. Make the event binding for that button. 'EVT_BUTTON(self, ID_CLEARBUTTON, self.OnClear)
4. Create the 'OnClear' method, something like:

def OnClear(self, event):
    self.clearButton.Clear()
    #self.clearButton.Refresh() ? not sure if you need this, or something like it

I also suggest that you split that class up into a bunch of smaller classes to make it easier to manage.

Cheers,
Jonathan

def OnClear(self, event):
   self.clearButton.Clear()

Oops, my mistake. You'll want to make the input field 'self.input' not just 'input'. Then this method runs self.input.Clear()

Cheers,
Jonathan

Here's an altered version of your code which I think does what you want.

Cheers,
Jonathan

test.py (13.8 KB)

Here's an altered version of your code which I think does what you want.

Cheers,
Jonathan

test.py (13.8 KB)

Is this a bug or am I living in the wrong timezone :?

>>> wx.VERSION
(2, 4, 2, 4, '')
>>> d = wx.DateTime_Now()
>>> d
<wxDateTime: "03/13/04 07:18:54" at _11fcf20_wxDateTime_p>
>>> d.GetDay()
13
>>> d.GetMonth()
2
>>> d.GetYear()
2004
>>>

-Joe

A lot of my users have been complaining about how, when Explorer crashes, the system tray icon disappears. Some have been constructive in their criticism, pointing out that I need to look for Explorers' broadcasting "TaskbarCreated" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_int/shell_int_programming/taskbar.asp). Can anyone tell me how to go about intercepting this message?