wx.TextCtrl - initial text value is only partially visible

371 views
Skip to first unread message

ErwinP

unread,
Nov 4, 2012, 6:44:57 AM11/4/12
to wxpytho...@googlegroups.com
Hello,

I am using two wx.TextCtrl's and I set a long text as the initial value of both controls with the SetValue method. The text from the first control is only partially visible (shifted to the left side), in the second control, the full text is visible. The first control has the focus and the text is selected. Here is my code:

---
import wx 

long_text = "Hello 0123456789012345678901234567890123456789"

class TestFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, -1, "Test") 
        panel = wx.Panel(self, -1) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        panel.SetSizer(sizer) 
        
        input0 = wx.TextCtrl(panel, -1) 
        sizer.Add(input0, 0, wx.EXPAND) 
        input0.SetValue(long_text)

        input1 = wx.TextCtrl(panel, -1) 
        sizer.Add(input1, 0, wx.EXPAND) 
        input1.SetValue(long_text)

app = wx.PySimpleApp() 
TestFrame().Show() 
app.MainLoop()
---

How can I enforce that the full text of the first control is visible ?

Versions:
wx 2.8.12.1
python 2.7.3
windows vista

Erwin

Robin Dunn

unread,
Nov 5, 2012, 3:58:39 PM11/5/12
to wxpytho...@googlegroups.com
On 11/4/12 3:44 AM, ErwinP wrote:
> Hello,
>
> I am using two wx.TextCtrl's and I set a long text as the initial value
> of both controls with the SetValue method. The text from the first
> control is only partially visible (shifted to the left side), in the
> second control, the full text is visible. The first control has the
> focus and the text is selected. Here is my code:

>
> How can I enforce that the full text of the first control is visible ?
>

Yeah, this is a quirk of the textctrl on windows that happens because by
default the whole content is selected and that leaves the caret at the
end, and it scrolls a bit to make sure the caret is visible. The
workaround is easy, just add something like this:

wx.CallAfter(theTextCtrl.SetInsertionPoint, 0)

--
Robin Dunn
Software Craftsman
http://wxPython.org

ErwinP

unread,
Nov 6, 2012, 3:09:43 PM11/6/12
to wxpytho...@googlegroups.com


Am Montag, 5. November 2012 21:58:43 UTC+1 schrieb Robin Dunn:
The workaround is easy, just add something like this:

     wx.CallAfter(theTextCtrl.SetInsertionPoint, 0)

Thanks for your answer. Unfortunately SetInsertion removes the selection. I use the following workaround which does what I want:

        input0.SetValue(long_text)
        def quirk_workaround(inp):
            selection = inp.GetSelection()
            inp.SetInsertionPoint(inp.GetInsertionPoint())
            inp.SetSelection(*selection) 
        wx.CallAfter(lambda: quirk_workaround(input0))

Erwin

Reply all
Reply to author
Forward
0 new messages