[Tutor] wxPython - how to 'focus'?

Hanna Joo hanna@chagford.com
Mon, 2 Jul 2001 10:20:09 -0700


Hello all

I am trying to build in 'LostFocus' and 'GotFocus' (Visual Basic terms, I
think) into my simple wxPython program. So far, I managed to find two
entries on events re 'Focus':
-- EVT_SET_FOCUS and EVT_KILL_FOCUS
there are a few other methods from wxWindow class: onSetFocus and
OnKillFocus. What I want to do is quite simple. I want the second textbox to
set its value when the first textbox loses focus.

from wxPython.wx import *

class Frame1(wxFrame):
 def __init__(self, parent, ID,title, pos, size):
  wxFrame.__init__(self, parent, ID, title, pos, size)
  self.panel1 = wxPanel(self, -1)
  self.tbox1 = wxTextCtrl(size = wxSize(224, 21), value = '', pos =
wxPoint(208, 48), parent = self.panel1, name = 'tbox1', style =
wxTE_PROCESS_TAB,id = 100)
  self.tbox2 = wxTextCtrl(size = wxSize(224,21), value = '', pos =
wxPoint(208,88),parent = self.panel1, name = 'tbox2', style =
wxTE_PROCESS_TAB, id = 101)

  self.text1 = wxStaticText(label = 'Hello1', id = -1, parent = self.panel1,
name = 'text1', size = wxSize(88, 24), style = 0, pos = wxPoint( 56, 48))
  self.text2 = wxStaticText(label = 'Hello2', id = -1, parent = self.panel1,
name = 'text2', size = wxSize(88, 24), style = 0, pos = wxPoint( 56, 88))


  button1 = wxButton(self.panel1, 1001, "See what happens!!")
  button1.SetPosition(wxPoint(200, 140))

  EVT_SET_FOCUS(self,self.Focus_tbox1)  ==> this hangs my machine completely

if I pass (self, 100 (id of textbox1), self.Focus_tbox1), it complains that
there are too many arguments.
  EVT_BUTTON(self, 1001,self.Button_down)

 def Focus_tbox1(self, event):

  self.tbox2.SetValue("My Text")

 def Button_down(self, event):
   self.Destroy()


class MyApp(wxApp):

 def OnInit(self):
  self.frame = Frame1(NULL, -1, "Focus Testing....", wxDefaultPosition,
wxSize(550,230))
  self.frame.Show(true)
  return true

app = MyApp(0)
app.MainLoop()

Do I have to instantiate my own event such as My_EVT=
wxFocusEvent.wxFocusEvent(EVT_SET_FOCUS, Tbox1.NoFocus)?
Hm, that doesn't work...

mmmm any help please?

A second question...

If I run this app, the first time I can enter down to the button. After
having gone down to the button, enter does not allow me to go to the next
field anymore. Only tab moves the cursor to the next control. I wonder why
this is so...