Hello !
Platform w2k and wxPython 2.3.3.1 on Python 2.2.
I have a problem with setting focus on controls , usually (wxPython 2.3.2) SetFocus works correct.
Since I installed wxPython 2.3.3.1 , I’ve met this problem , SetFocus and SetDefault don’t work.
I have tried SetFocus() in every possible places but this doesn’t help at all.
In this example I would like to set focus on wxTextControl named “self.tekst” , but the focus is on the button self.ok which is also default button , instruction self.anuluj.SetDefault() should make this button (self.anuluj) as default but it doesn’t.
Where have I made the mistake ?
Pls find below simple example with wxDialog.
···
from wxPython.wx import *
class xDialog(wxDialog):
def init (self,parent,id = -1 ,title=“Pole:”,pos=(-1,-1) ,size=(400,150), len=1,opis = ‘?’,value=’’):
self.parent = parent
wxDialog.init(self,parent,id,title,pos,size,style = wxDEFAULT_DIALOG_STYLE) #|wxRESIZE_BORDER)
self.CentreOnParent(wxBOTH)
self.out = None
self.akcja = None
self.opis = opis
self.len = len
self.value = value
if wxPlatform == ‘WXMSW’:
self.font =wxFont(pointSize=10,family=wxDEFAULT,style=wxNORMAL,weight=wxNORMAL,
faceName = “Courier”,encoding = wxFONTENCODING_CP1250 )
else:
self.font = wxFont(pointSize=10,family=wxDEFAULT,style=wxNORMAL,weight=wxNORMAL,faceName = “Courier”)
EVT_CHAR_HOOK (self,self.OnHook)
EVT_SIZE(self,self.OnSize)
rozmiar = self.GetClientSize()
self.ok = wxButton(self,wxID_OK,label=‘OK’)
self.anuluj = wxButton(self,wxID_CANCEL,label=‘Anuluj’)
self.linia = wxStaticLine(self,-1,size = (self.GetClientSizeTuple() [0],2), pos=(1,125),style=wxLI_HORIZONTAL)
create others controls
self.Create()
EVT_BUTTON(self,self.ok.GetId(),self.OnOK)
EVT_BUTTON(self,self.anuluj.GetId(),self.OnAnuluj)
self.anuluj.SetDefault() # it doesn’t work ! - why ?
lc = wxLayoutConstraints()
lc.bottom.SameAs(self, wxBottom, 10)
lc.right.RightOf (self, -10)
lc.height.AsIs ()
lc.width.AsIs ()
self.anuluj.SetConstraints(lc)
lc = wxLayoutConstraints()
lc.bottom.SameAs(self.anuluj, wxBottom)
lc.right.LeftOf (self.anuluj, 10)
lc.height.AsIs ()
lc.width.AsIs ()
self.ok.SetConstraints(lc)
lc = wxLayoutConstraints()
lc.bottom.Above(self.anuluj, -10)
lc.left.SameAs (self,wxLeft,5)
lc.right.SameAs (self,wxRight,5)
lc.height.AsIs ()
self.linia.SetConstraints(lc)
self.tekst.SetFocus() # it doesn’t work ! - why ?
self.Layout()
self.tekst.SetFocus() # it doesn’t work ! - why ?
def OnHook(self,event):
if event.m_keyCode == WXK_ESCAPE :
self.OnAnuluj(event)
event.Skip()
def Create(self):
self.opis = wxStaticText(self,-1,label = self.opis , pos = (10,10) )
self.tekst = wxTextCtrl(self,-1, size = (-1 , 25))
self.tekst.SetMaxLength(self.len)
self.tekst.SetFont(self.font)
self.tekst.SetFocus() # it doesn’t work ! - why ?
lc = wxLayoutConstraints()
lc.top.Below(self.opis, 10)
lc.left.SameAs (self,wxLeft,10)
lc.right.SameAs (self,wxRight,10)
lc.height.AsIs ()
self.tekst.SetConstraints(lc)
def GetOut(self):
return self.tekst.GetValue()
def OnOK(self,event):
self.out = self.GetOut()
self.akcja = wxID_OK
self.EndModal(wxID_OK)
def OnAnuluj(self,event):
self.out = None
self.akcja = wxID_CANCEL
self.EndModal(wxID_CANCEL)
def OnSize(self,event):
self.Layout()
self.Refresh()
def ask(self):
self.Centre()
self.ShowModal()
tmp=None
if self.akcja == wxID_OK:
return self.out
else:
return tmp
if name == “main”:
class MyApp(wxApp):
def OnInit(self):
tytul = ‘xxx’
opis = ‘yyy’
w = xDialog(None, -1, title= tytul, opis=opis)
w.tekst.SetFocus() # it also doesn’t work ! - why ?
out =w.ask()
w.Destroy()
return true
app = MyApp(0)
app.MainLoop()
Thanks for any suggestions.
Artur