scrolledpanel

Bard S. Selbekk wrote:

Hi, fellow programmers!

ScrolledPanel has the wonderful automatic tab traversal
so that when you tab into a textcontrol inside it, it
automatically scrolles to make the textcontrol with
focus visible.

However, when my program makes a new textcontrol and
adds it to the sizer, and I give it focus, there is no
automatic scrolling.

The scrolledpanel magic is performed when it gets a child focus event, but when you explicitly call SetFocus there is not a child focus event.

Is there any elegant way to make scrolledpanel scroll
to make the textcontrol with focus visible?

I'll refactor ScrolledPanel so you can call a method yourself in situations like this, but in the meantime it may be easiest to just send the child focus event yourself.

import wx
import wx.lib.scrolledpanel

class MyFrame(wx.Frame):
     def __init__(self, parent, id, title,
                  pos = wx.DefaultPosition, size = wx.DefaultSize,
                  style = wx.DEFAULT_FRAME_STYLE ):
         wx.Frame.__init__(self, parent, id, title, pos, size, style)

         self.scroll = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
         self.mainsizer = wx.BoxSizer( wx.VERTICAL )
         ADDBUTTON = wx.Button( self.scroll, -1, "&Add",
                                wx.DefaultPosition, wx.DefaultSize, 0 )
         self.mainsizer.Add( ADDBUTTON, 0, wx.ALIGN_CENTER )
         self.scroll.SetSizer(self.mainsizer)
         self.scroll.SetupScrolling(scroll_x=False)

         self.Bind(wx.EVT_BUTTON, self.OnAddClick, ADDBUTTON)

         self.MakeParagraph( self.scroll, self.mainsizer, 13)

     def OnAddClick(self, event):
         lastWin = self.MakeParagraph( self.scroll, self.mainsizer, 7)
         self.scroll.SetupScrolling(scroll_x=False)
         wx.CallAfter(self.SetFocusAndScroll, lastWin)

     def MakeParagraph( self, parent, sizer, numfields ):
         mygridsizer = wx.GridSizer( 0, 4, 0, 0 )
         mylist=
         for i in range(0,numfields):
             mylist.append(wx.TextCtrl( parent, -1, "",
                                        wx.DefaultPosition,
                                        [80,-1], 0 ))
             mygridsizer.Add( mylist[i], 0,
                              wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
         sizer.Add( mygridsizer, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL, 5 )
         return mylist[i]

     def SetFocusAndScroll(self, win):
         win.SetFocus()
         evt = wx.ChildFocusEvent(win)
         win.GetEventHandler().ProcessEvent(evt)

class MyApp(wx.App):

     def OnInit(self):
         wx.InitAllImageHandlers()
         frame = MyFrame( None, -1, "MyApp",
                          wx.DefaultPosition, [400,200] )
         frame.Show(True)
         return True

app = MyApp(0)
app.MainLoop()

ยทยทยท

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!