2 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
6 text
= "one two buckle my shoe three four shut the door five six pick up sticks seven eight lay them straight nine ten big fat hen"
9 class ScrolledPanel(wxScrolledWindow
):
10 def __init__(self
, parent
, log
):
12 wxScrolledWindow
.__init
__(self
, parent
, -1,
13 style
= wxTAB_TRAVERSAL
)
15 box
= wxBoxSizer(wxVERTICAL
)
16 box
.Add(wxStaticText(self
, -1,
17 "This sample shows how to make a scrollable data entry \n"
18 "form by using a wxSizer in a wxScrolledWindow."),
20 box
.Add(wxStaticLine(self
, -1), 0, wxEXPAND|wxALL
, 5)
22 fgs
= wxFlexGridSizer(cols
=2, vgap
=4, hgap
=4)
29 for word
in text
.split():
30 label
= wxStaticText(self
, -1, word
+":")
31 tc
= wxTextCtrl(self
, -1, word
)
32 fgs
.Add(label
, flag
=wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL
)
33 fgs
.Add(tc
, flag
=wxEXPAND
)
36 box
.Add(10, 40) # some more empty space at the bottom
40 # The following is all that is needed to integrate the sizer and the
41 # scrolled window. In this case we will only support vertical scrolling.
42 self
.EnableScrolling(false
, true
)
43 self
.SetScrollRate(0, 20)
44 box
.SetVirtualSizeHints(self
)
45 EVT_CHILD_FOCUS(self
, self
.OnChildFocus
)
47 wxCallAfter(self
.Scroll
, 0, 0) # scroll back to top after initial events
50 def OnChildFocus(self
, evt
):
51 # If the child window that gets the focus is not visible,
52 # this handler will try to scroll enough to see it. If you
53 # need to handle horizontal auto-scrolling too then this will
56 child
= evt
.GetWindow()
58 sppu_y
= self
.GetScrollPixelsPerUnit()[1]
59 vs_y
= self
.GetViewStart()[1]
60 cpos
= child
.GetPosition()
63 # is it above the top?
65 new_vs
= cpos
.y
/ sppu_y
66 self
.Scroll(-1, new_vs
)
68 # is it below the bottom ?
69 if cpos
.y
+ csz
.height
> self
.GetClientSize().height
:
70 diff
= (cpos
.y
+ csz
.height
- self
.GetClientSize().height
) / sppu_y
71 self
.Scroll(-1, vs_y
+ diff
+ 1)
74 #----------------------------------------------------------------------
77 def runTest(frame
, nb
, log
):
78 win
= ScrolledPanel(nb
, log
)
81 #----------------------------------------------------------------------
85 overview
= """<html><body>
86 This sample shows how to make a scrollable data entry form by
87 using a wxSizer in a wxScrolledWindow.
93 if __name__
== '__main__':
96 run
.main(['', os
.path
.basename(sys
.argv
[0])])