]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTextCtrl.py
2 from wxPython
.wx
import *
4 #---------------------------------------------------------------------------
6 class TestPanel(wxPanel
):
7 def OnSetFocus(self
, evt
):
10 def OnKillFocus(self
, evt
):
13 def OnWindowDestroy(self
, evt
):
14 print "OnWindowDestroy"
18 def __init__(self
, parent
, log
):
19 wxPanel
.__init
__(self
, parent
, -1)
22 l1
= wxStaticText(self
, -1, "wxTextCtrl")
23 t1
= wxTextCtrl(self
, 10, "Test it out and see", size
=(125, -1))
24 t1
.SetInsertionPoint(0)
25 EVT_TEXT(self
, 10, self
.EvtText
)
26 EVT_CHAR(t1
, self
.EvtChar
)
27 EVT_SET_FOCUS(t1
, self
.OnSetFocus
)
28 EVT_KILL_FOCUS(t1
, self
.OnKillFocus
)
29 EVT_WINDOW_DESTROY(t1
, self
.OnWindowDestroy
)
31 l2
= wxStaticText(self
, -1, "Passsword")
32 t2
= wxTextCtrl(self
, 20, "", size
=(125, -1), style
=wxTE_PASSWORD
)
33 EVT_TEXT(self
, 20, self
.EvtText
)
35 l3
= wxStaticText(self
, -1, "Multi-line")
36 t3
= wxTextCtrl(self
, 30,
37 "Here is a looooooooooooooong line of text set in the control.\n\n"
38 "The quick brown fox jumped over the lazy dog...",
39 size
=(200, 100), style
=wxTE_MULTILINE
)
40 t3
.SetInsertionPoint(0)
41 EVT_TEXT(self
, 30, self
.EvtText
)
42 b
= wxButton(self
, -1, "Test Replace")
43 EVT_BUTTON(self
, b
.GetId(), self
.OnTestReplace
)
44 b2
= wxButton(self
, -1, "Test GetSelection")
45 EVT_BUTTON(self
, b2
.GetId(), self
.OnTestGetSelection
)
48 l4
= wxStaticText(self
, -1, "Rich Text")
49 t4
= wxTextCtrl(self
, 40, "If supported by the native control, this is red, and this is a different font.",
50 size
=(200, 100), style
=wxTE_MULTILINE|wxTE_RICH
)
51 t4
.SetInsertionPoint(0)
52 t4
.SetStyle(44, 47, wxTextAttr("RED", "YELLOW"))
54 points
= t4
.GetFont().GetPointSize() # get the current size
55 f
= wxFont(points
+3, wxROMAN
, wxITALIC
, wxBOLD
, true
)
56 ## print 'a1', sys.getrefcount(f)
57 ## t4.SetStyle(63, 77, wxTextAttr("BLUE", font=f))
58 t4
.SetStyle(63, 77, wxTextAttr("BLUE", wxNullColour
, f
))
59 ## print 'a2', sys.getrefcount(f)
61 bsizer
= wxBoxSizer(wxVERTICAL
)
62 bsizer
.Add(b
, 0, wxGROW
)
63 bsizer
.Add(b2
, 0, wxGROW
)
65 sizer
= wxFlexGridSizer(cols
=3, hgap
=6, vgap
=6)
66 sizer
.AddMany([ l1
, t1
, (0,0),
71 border
= wxBoxSizer(wxVERTICAL
)
72 border
.Add(sizer
, 0, wxALL
, 25)
74 self
.SetAutoLayout(true
)
77 def EvtText(self
, event
):
78 self
.log
.WriteText('EvtText: %s\n' % event
.GetString())
81 def EvtChar(self
, event
):
82 self
.log
.WriteText('EvtChar: %d\n' % event
.GetKeyCode())
86 def OnTestReplace(self
, evt
):
87 self
.tc
.Replace(5, 9, "IS A")
90 def OnTestGetSelection(self
, evt
):
91 start
, end
= self
.tc
.GetSelection()
92 text
= self
.tc
.GetValue()
93 if wxPlatform
== "__WXMSW__": # This is why GetStringSelection was added
94 text
= string
.replace(text
, '\n', '\r\n')
95 self
.log
.write("GetSelection(): (%d, %d)\n"
96 "\tGetStringSelection(): %s\n"
97 "\tSelectedText: %s\n" %
99 self
.tc
.GetStringSelection(),
100 repr(text
[start
:end
])))
103 #---------------------------------------------------------------------------
105 def runTest(frame
, nb
, log
):
106 win
= TestPanel(nb
, log
)
109 #---------------------------------------------------------------------------