]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTextCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
9 #---------------------------------------------------------------------------
11 class TestPanel(wx
.Panel
):
12 def OnSetFocus(self
, evt
):
15 def OnKillFocus(self
, evt
):
18 def OnWindowDestroy(self
, evt
):
19 print "OnWindowDestroy"
23 def __init__(self
, parent
, log
):
24 wx
.Panel
.__init
__(self
, parent
, -1)
27 l1
= wx
.StaticText(self
, -1, "wx.TextCtrl")
28 t1
= wx
.TextCtrl(self
, -1, "Test it out and see", size
=(125, -1))
29 t1
.SetInsertionPoint(0)
32 self
.Bind(wx
.EVT_TEXT
, self
.EvtText
, t1
)
33 t1
.Bind(wx
.EVT_CHAR
, self
.EvtChar
)
34 t1
.Bind(wx
.EVT_SET_FOCUS
, self
.OnSetFocus
)
35 t1
.Bind(wx
.EVT_KILL_FOCUS
, self
.OnKillFocus
)
36 t1
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.OnWindowDestroy
)
38 l2
= wx
.StaticText(self
, -1, "Password")
39 t2
= wx
.TextCtrl(self
, -1, "", size
=(125, -1), style
=wx
.TE_PASSWORD
)
40 self
.Bind(wx
.EVT_TEXT
, self
.EvtText
, t2
)
42 l3
= wx
.StaticText(self
, -1, "Multi-line")
43 t3
= wx
.TextCtrl(self
, -1,
44 "Here is a looooooooooooooong line of text set in the control.\n\n"
45 "The quick brown fox jumped over the lazy dog...",
46 size
=(200, 100), style
=wx
.TE_MULTILINE
)
48 t3
.SetInsertionPoint(0)
49 self
.Bind(wx
.EVT_TEXT
, self
.EvtText
, t3
)
50 b
= wx
.Button(self
, -1, "Test Replace")
51 self
.Bind(wx
.EVT_BUTTON
, self
.OnTestReplace
, b
)
52 b2
= wx
.Button(self
, -1, "Test GetSelection")
53 self
.Bind(wx
.EVT_BUTTON
, self
.OnTestGetSelection
, b2
)
54 b3
= wx
.Button(self
, -1, "Test WriteText")
55 self
.Bind(wx
.EVT_BUTTON
, self
.OnTestWriteText
, b3
)
59 l4
= wx
.StaticText(self
, -1, "Rich Text")
60 t4
= wx
.TextCtrl(self
, -1, "If supported by the native control, this is red, and this is a different font.",
61 size
=(200, 100), style
=wx
.TE_MULTILINE|wx
.TE_RICH2
)
62 t4
.SetInsertionPoint(0)
63 t4
.SetStyle(44, 47, wx
.TextAttr("RED", "YELLOW"))
64 points
= t4
.GetFont().GetPointSize() # get the current size
65 f
= wx
.Font(points
+3, wx
.ROMAN
, wx
.ITALIC
, wx
.BOLD
, True)
66 t4
.SetStyle(63, 77, wx
.TextAttr("BLUE", wx
.NullColour
, f
))
68 l5
= wx
.StaticText(self
, -1, "Test Positions")
69 t5
= wx
.TextCtrl(self
, -1, "0123456789\n" * 5, size
=(200, 100),
70 style
= wx
.TE_MULTILINE
74 t5
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnT5LeftDown
)
77 bsizer
= wx
.BoxSizer(wx
.VERTICAL
)
78 bsizer
.Add(b
, 0, wx
.GROW|wx
.ALL
, 4)
79 bsizer
.Add(b2
, 0, wx
.GROW|wx
.ALL
, 4)
80 bsizer
.Add(b3
, 0, wx
.GROW|wx
.ALL
, 4)
82 sizer
= wx
.FlexGridSizer(cols
=3, hgap
=6, vgap
=6)
83 sizer
.AddMany([ l1
, t1
, (0,0),
89 border
= wx
.BoxSizer(wx
.VERTICAL
)
90 border
.Add(sizer
, 0, wx
.ALL
, 25)
92 self
.SetAutoLayout(True)
95 def EvtText(self
, event
):
96 self
.log
.WriteText('EvtText: %s\n' % event
.GetString())
99 def EvtChar(self
, event
):
100 self
.log
.WriteText('EvtChar: %d\n' % event
.GetKeyCode())
104 def OnTestReplace(self
, evt
):
105 self
.tc
.Replace(5, 9, "IS A")
106 #self.tc.Remove(5, 9)
108 def OnTestWriteText(self
, evt
):
109 self
.tc
.WriteText("TEXT")
111 def OnTestGetSelection(self
, evt
):
112 start
, end
= self
.tc
.GetSelection()
113 text
= self
.tc
.GetValue()
114 if wx
.Platform
== "__WXMSW__": # This is why GetStringSelection was added
115 text
= text
.replace('\n', '\r\n')
117 self
.log
.write("multi-line GetSelection(): (%d, %d)\n"
118 "\tGetStringSelection(): %s\n"
119 "\tSelectedText: %s\n" %
121 self
.tc
.GetStringSelection(),
122 repr(text
[start
:end
])))
124 start
, end
= self
.tc1
.GetSelection()
125 text
= self
.tc1
.GetValue()
127 if wx
.Platform
== "__WXMSW__": # This is why GetStringSelection was added
128 text
= text
.replace('\n', '\r\n')
130 self
.log
.write("single-line GetSelection(): (%d, %d)\n"
131 "\tGetStringSelection(): %s\n"
132 "\tSelectedText: %s\n" %
134 self
.tc1
.GetStringSelection(),
135 repr(text
[start
:end
])))
138 def OnT5LeftDown(self
, evt
):
140 wx
.CallAfter(self
.LogT5Position
, evt
)
142 def LogT5Position(self
, evt
):
143 text
= self
.t5
.GetValue()
144 ip
= self
.t5
.GetInsertionPoint()
145 lp
= self
.t5
.GetLastPosition()
146 self
.log
.write("LogT5Position:\n"
147 "\tGetInsertionPoint:\t%d\n"
148 "\ttext[insertionpoint]:\t%s\n"
149 "\tGetLastPosition:\t%d\n"
150 "\tlen(text):\t\t%d\n"
151 % (ip
, text
[ip
], lp
, len(text
)))
154 #---------------------------------------------------------------------------
156 def runTest(frame
, nb
, log
):
157 win
= TestPanel(nb
, log
)
160 #---------------------------------------------------------------------------
164 A text control allows text to be displayed and (possibly) edited. It may be single
165 line or multi-line, support styles or not, be read-only or not, and even supports
166 text masking for such things as passwords.
172 if __name__
== '__main__':
175 run
.main(['', os
.path
.basename(sys
.argv
[0])])