0

# Other field creations here....

self.Sfield1 = wx.TextCtrl(id=wxID_SEARCHBROWSERSFIELD1,
name=u'Sfield1', parent=self, pos=wx.Point(144, 80),
size=wx.Size(216, 21), style=0, value=u'')
self.Sfield1.SetToolTipString(u'')

self.staticText1 = wx.StaticText(id=wxID_SEARCHBROWSERSTATICTEXT1,
label=u'Customer N\xb0', name='staticText1', parent=self,
pos=wx.Point(32, 80), size=wx.Size(83, 16), style=0)
self.staticText1.SetForegroundColour(wx.Colour(255, 255, 255))
self.staticText1.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL,
False, u'Verdana'))
self.staticText1.SetToolTipString(u'')
self.staticText1.SetBackgroundColour(wx.Colour(0, 0, 128))
self.Sfield1.SetFocus()


Why isn't the focus set to SField1 ???????

tia

Rony

2
Contributors
6
Replies
10
Views
12 Years
Discussion Span
Last Post by vegaseat
0

wx.TextCtrl does not have a member/method SetFocus() and has to get it from it's parent like a wx.Frame or wx.Panel.

So put the wx.TextCtrl on a wx.Panel or wx.Frame and it will work!

# 3 editboxes on a frame

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'wx.TextCtrl and SetFocus()', size=(400, 150))
        self.SetBackgroundColour('green')
        
        self.edit1 = wx.TextCtrl(self, -1, value="", pos=(10, 10), size=(300, 25))
        self.edit2 = wx.TextCtrl(self, -1, value="", pos=(10, 40), size=(300, 25))
        self.edit3 = wx.TextCtrl(self, -1, value="", pos=(10, 70), size=(300, 25))
        
        self.edit2.SetFocus()  # editbox is on a frame so this works

        # show the frame
        self.Show(True)


application = wx.PySimpleApp()
# instantiate class MyFrame
window = MyFrame()
# start the event loop
application.MainLoop()
0

I did the same thing using the Boa Constructor program ...

# In Boa Constructor select "New" "wx.Frame" 
# in the source display select "Edit" and "Add simple app"
# this way all code is in one file

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, 
 wxID_FRAME1TEXTCTRL3, 
] = [wx.NewId() for _init_ctrls in range(4)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(315, 133), size=wx.Size(400, 190),
              style=wx.DEFAULT_FRAME_STYLE, title=u'wx.TextCtrl and SetFocus')
        self.SetClientSize(wx.Size(392, 150))
        self.SetBackgroundColour(wx.Colour(0, 128, 0))

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self, pos=wx.Point(16, 24), size=wx.Size(344, 24), style=0,
              value=u'')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self, pos=wx.Point(16, 64), size=wx.Size(344, 24), style=0,
              value=u'')

        self.textCtrl3 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3, name='textCtrl3',
              parent=self, pos=wx.Point(16, 104), size=wx.Size(344, 24),
              style=0, value=u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.textCtrl2.SetFocus()  # parent is frame, this will work now


if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    frame.Show()

    app.MainLoop()
0

I did the same thing using the Boa Constructor program ...

# In Boa Constructor select "New" "wx.Frame" 
# in the source display select "Edit" and "Add simple app"
# this way all code is in one file

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, 
 wxID_FRAME1TEXTCTRL3, 
] = [wx.NewId() for _init_ctrls in range(4)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(315, 133), size=wx.Size(400, 190),
              style=wx.DEFAULT_FRAME_STYLE, title=u'wx.TextCtrl and SetFocus')
        self.SetClientSize(wx.Size(392, 150))
        self.SetBackgroundColour(wx.Colour(0, 128, 0))

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self, pos=wx.Point(16, 24), size=wx.Size(344, 24), style=0,
              value=u'')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self, pos=wx.Point(16, 64), size=wx.Size(344, 24), style=0,
              value=u'')

        self.textCtrl3 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3, name='textCtrl3',
              parent=self, pos=wx.Point(16, 104), size=wx.Size(344, 24),
              style=0, value=u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.textCtrl2.SetFocus()  # parent is frame, this will work now


if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    frame.Show()

    app.MainLoop()

Thanks a lot for this boa example. When I try it, it works and the focus is initialy placed in the second entry control, which I asked. But now I noticed that the TAB key to navigate between the controls don't work.
And I still don't see , if in a later stade of development, we want to insert a textcontrol between 1 and 2, because one was forgotten, how we can do this.

I'm used to tkinter where all this 'seems' a lot easier, I could be wrong of course but is it that difficult to create a standard windows application with wx ?

tia

Rony

0

Sorry, putting components on a frame was the simplest solution to your original problem. Normally, I put the components on a panel which gives the highest flexibility uncluding moving the focus with a tab key. Here is the boa generated code ...

# In Boa Constructor select "New" "wx.Frame" 
# in the source display select "Edit" and "Add simple app"
# this way all code is in one file
# now add the panel and drop the three editboxes on the panel

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1PANEL1, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, 
 wxID_FRAME1TEXTCTRL3, 
] = [wx.NewId() for _init_ctrls in range(5)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(332, 135), size=wx.Size(400, 190),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 150))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(392, 150),
              style=wx.TAB_TRAVERSAL)
        self.panel1.SetBackgroundColour(wx.Colour(0, 128, 0))

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(16, 16), size=wx.Size(344, 24),
              style=0, value='textCtrl1')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self.panel1, pos=wx.Point(16, 56), size=wx.Size(344, 24),
              style=0, value='textCtrl2')

        self.textCtrl3 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3, name='textCtrl3',
              parent=self.panel1, pos=wx.Point(16, 96), size=wx.Size(344, 24),
              style=0, value='textCtrl3')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.textCtrl2.SetFocus()  # parent is panel, focus and tab work now


if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    frame.Show()

    app.MainLoop()

I find boa too frustrating for simple stuff and do it from scratch in my DrPython IDE. A similar code will look like this ...

# 3 editboxes on a panel
# when panel is the parent the tab key switches to the next control

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("green")
        
        self.edit1 = wx.TextCtrl(self, -1, value="", pos=(10, 10), size=(300, 25))
        self.edit2 = wx.TextCtrl(self, -1, value="", pos=(10, 40), size=(300, 25))
        self.edit3 = wx.TextCtrl(self, -1, value="", pos=(10, 70), size=(300, 25))
        
        self.edit2.SetFocus()  # editbox is on a panel so focus and tab-focus-move work
        
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "wx.TextCtrl and SetFocus()", size = (400, 150))
# instantiate class MyPanel
MyPanel(frame,-1)
frame.Show(True)
app.MainLoop()
0

Thanks for the quick answer !

Actually what you're saying is that BOA makes it complicated ans I agree, you code looks realy a lot simpler for doing the same thing...

Rony

0

Boa code not only looks more complicated, but on simple apps when you reload suddenly the frame designer goes out and leaves you stuck! There are other frustrating behaviours that I hope will be corrected in later more stable versions of the boa program. I keep templates of basic wxPython code (non-boa) and start from there.

Edit: Have to eat crow! Figured out the problem with the disappearing frame designer button. Don't add any comments above the line

#Boa:Frame:Frame1

boa does not like that!!! The line looks like a comment, but is actually a directive and has to come first! Boa is in my good graces again!

Just a little note on the Tkinter GUI. It is simpler than wxPython, but so is the appearance of the controls, they look a little dated! Some people call them ugly!

I think wxPython has a much steeper learning curve due to the clumsiness of the tutorial examples. Manual notations seem to laps back to wxPython's C++ origin, a little confusing at best.

Note: The first wxPython book is supposed to appear in January 2006 "wxPython in Action" by
Noel Rappin and Robin Dunn.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.