SetSelection in wx.textCtrl doesn't work !

Hi all

(Sorry for my english, im french)

I've an amazing problem with a wx.textCtrl (!)

I've taken this code on wiki.wxPython ("combobox that suggets
options") and adapts it on a textCtrl. Easy...
But wx.TextCtrl.SetSelection(...) works on Windows but NOT ON LINUX
(Ubuntu). I use last version of wxPython.

You can try yourself this little code on Linux to reproduce the bug (i
hope) :

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import wx

class PromptingTextCtrl(wx.TextCtrl) :
    def __init__(self, parent, value, choices):
        wx.TextCtrl.__init__(self, parent, wx.ID_ANY, value, size=
(400, -1))
        self.choices = choices
        self.Bind(wx.EVT_TEXT, self.EvtText)
        self.Bind(wx.EVT_CHAR, self.EvtChar)
        self.ignoreEvtText = False

    def EvtChar(self, event):
        if event.GetKeyCode() == 8:
            self.ignoreEvtText = True
        event.Skip()

    def EvtText(self, event):
        if self.ignoreEvtText:
            self.ignoreEvtText = False
            return
        currentText = event.GetString()
        found = False
        for choice in self.choices :
            if choice.startswith(currentText):
                self.ignoreEvtText = True
                self.SetValue(choice)
                self.SetInsertionPoint(len(currentText))
                # HERE HIGHLIGHT SELECTION DOESNT APPEAR !!!!!!!
                self.SetSelection(len(currentText), len(choice))
                found = True
                break
        if not found:
            event.Skip()

class TrialPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        choices = ['grandmother', 'grandfather', 'cousin', 'aunt',
'uncle', 'grandson', 'granddaughter']
        for relative in ['mother', 'father', 'sister', 'brother',
'daughter', 'son']:
            choices.extend(self.derivedRelatives(relative))
        cb = PromptingTextCtrl(self, "default value", choices)
    def derivedRelatives(self, relative):
        return [relative, 'step' + relative, relative + '-in-law']

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame (None, -1, 'Demo PromptingTextCtrl Control', size=
(500, 50))
    TrialPanel(frame)
    frame.Show()
    app.MainLoop()

I really hope you'll be able to help me !!

Thx for your answer.

Some things in GTK are not fully updated immediately but rather some time after the call when the results from the first part of the operation are returned from the X-Sever. This means that sometimes the 2nd half of some operation can actually happen after you have tried to do it yourself in some other way, which essentially undoes what you wanted done.

In this case I expect that either the SetValue or the normal processing of the user's typing is setting the selection after you have already called SetSelection, so you need to delay your call to SetSelection so it happens after GTK does its thing. Try using wx.CallAfter or wx.CallLater with a short timeout and see how that works.

BTW, wx.TextCtrl now has a ChangeValue method that does not cause events to be sent, so you can use that instead of SetValue and not have to worry about blocking reentrancy.

···

On 9/30/09 12:25 PM, Gg4d wrote:

Hi all

(Sorry for my english, im french)

I've an amazing problem with a wx.textCtrl (!)

I've taken this code on wiki.wxPython ("combobox that suggets
options") and adapts it on a textCtrl. Easy...
But wx.TextCtrl.SetSelection(...) works on Windows but NOT ON LINUX
(Ubuntu). I use last version of wxPython.

--
Robin Dunn
Software Craftsman

Thx for the answer...

But i tried with "ChangeValue" instead of "SetValue" + "wx.CallAfter",
"wx.CallLater", "wx.FutureCall"... But it still doesn't work :frowning:

Would you have another idea for me ?