wx.Caret vanishes

I'm writing an editor and just replaced my home-rolled caret
with wx.caret. It vanishes after running a dialog, even though
.IsVisible returns true. Calling .Move, .Show etc. don't help.
Sample app attached (and here).

Thanks in advance for any clues, Phil

import wx

action = """Click on the button,
then cancel the dialog.
The cursor will vanish.
Using wx 2.8.7.1
Python 2.5.2
"""

class Dlg(wx.Dialog):
     def __init__(self):
         wx.Dialog.__init__(self, None, -1, u"wx.Caret problem")
         sizer = wx.BoxSizer(wx.VERTICAL)
         sizer.Add(wx.StaticText(self, -1, action), 0, wx.ALL, 5)
         win = wx.Window(self, -1, size=(100,100))
         self.caret = wx.Caret(win, (4, 20))
         sizer.Add(win, 0, wx.ALL, 5)
         btn = wx.Button(self, -1, "Click me")
         sizer.Add(btn, 0, wx.ALL, 5)
         btn.Bind(wx.EVT_BUTTON, self.OnClick)
         self.SetSizer(sizer)
         sizer.Fit(self)
         wx.CallAfter(self.MoveCaret, 10,10)

     def OnClick(self, evt):
         dlg = wx.FileDialog(None, "Caret will vanish", "", "")
         dlg.ShowModal()
         dlg.Destroy()

     def MoveCaret(self, x,y):
         if self.caret.IsVisible():
             self.caret.Hide()
         self.caret.Move((x, y))
         self.caret.Show()

if __name__ == '__main__':
     app = wx.PySimpleApp()
     Dlg().ShowModal()

testcaret.py (1.11 KB)

Hello,

···

On Tue, Mar 3, 2009 at 1:12 PM, Phil Mayes olivebr@olivebr.com wrote:

I’m writing an editor and just replaced my home-rolled caret
with wx.caret. It vanishes after running a dialog, even though

.IsVisible returns true. Calling .Move, .Show etc. don’t help.
Sample app attached (and here).

Thanks in advance for any clues, Phil
I am seeing two things.

  1. You need to call win.SetCaret(self.caret) to associate the caret with the window object.

  2. The window must have the focus for the caret to be shown. After clicking on the button the button will have the focus. You can either set it back programatically or click in the windows area to give it the focus back after closing your dialog.

Cody

Thank you, Cody, I didn’t know about SetCaret. Just passing the
window in the caret’s constructor made my app /almost/ work.

One more tip for any other readers: SetCaret was ineffective when called
from init; I had to use CallAfter.

Phil

···

At 11:48 AM 3/3/2009, Cody wrote:

Hello,

On Tue, Mar 3, 2009 at 1:12 PM, Phil Mayes > olivebr@olivebr.com > wrote:
I’m writing an editor and just replaced my home-rolled caret
with wx.caret. It vanishes after running a dialog, even
though
.IsVisible returns true. Calling .Move, .Show etc. don’t
help.
Sample app attached (and here).

Thanks in advance for any clues, Phil

I am seeing two things.

  1. You need to call win.SetCaret(self.caret) to associate the caret with
    the window object.

  2. The window must have the focus for the caret to be shown. After
    clicking on the button the button will have the focus. You can either set
    it back programatically or click in the windows area to give it the focus
    back after closing your dialog.