1

Here's my problem:

enter image description here

self.textCtrl1 = wx.TextCtrl(id=wx.ID_ANY, name='textCtrl1',
              parent=self, pos=wx.Point(0, 100), size=wx.Size(400, 100),
              style=wx.VSCROLL | wx.TE_MULTILINE, value='')
self.textCtrl1.SetValue("I don't want this line to be selected by default!\n")

As you can see, each time the dialog starts, content in wx.TextCtrl is always being selected by default, even when I SetInsertionPoint to the last character. This really is annoying, since anything you type in would erase previously selected content by default, and I couldn't find a way to set the cursor to the end of the initial text. So how do you fix this problem?

P.S.: I'm on win 7 64bit with wxPython 2.8.12.1, python 2.7.3

user2963623
  • 2,267
  • 1
  • 14
  • 25
Shane
  • 4,875
  • 12
  • 49
  • 87

3 Answers3

2

It's strange that your textCtrl text is automatically getting selected. Under normal circumstances the cursor would only be pointing at the start of the box.

Try one of these:

self.textCtrl.SetValue("I don't want this line to be selected by default!\n")
self.textCtrl.SetInsertionPoint(self.textCtrl.GetLastPosition())

or rather than using SetValue, use this:

print >> self.textCtrl, "I don't want this line to be selected by default!"
user2963623
  • 2,267
  • 1
  • 14
  • 25
  • I've tried all of above but none of them worked, this problem is really annoying... – Shane Jul 11 '14 at 16:31
  • You might try setting the value at the object creation in the value field. – user2963623 Jul 11 '14 at 16:32
  • It's still the same, those lines get selected by default every single time – Shane Jul 11 '14 at 16:36
  • :( That is very weird. Try creating a frame with a single textctrl widget and see if it shows the same behavior. If it does then its native to your computer. By the way your code worked fine on my machine. That's all I can think of – user2963623 Jul 11 '14 at 16:43
  • What system are you on? I've tried one textctrl widget frame with both win 7 and XP, and the results are the same: all contents are selected by default when the frame starts. This really pissed me off... – Shane Jul 11 '14 at 17:11
  • I'm on win 8.1. You might want to try updating wxPython to 3.0 – user2963623 Jul 11 '14 at 17:17
2

I had this same problem. I found that I had to set the value of the textctrl after I ran the frame.Show() method.

1

I've read some of the posts on the wxWidgets forum, and it seems that on Windows in wxWidgets 2.8x, multi-line text was, by default, auto-selected, and there was no good way to deselect it. It is said that this is the default behavior because it mirrors the default behavior of the Windows API, though I'm not sure why they would have a function called SetSelection() that doesn't actually set the selection. The wxWidgets posters had suggested maybe using the wxRichTextCtrl since that doesn't come up auto-selected.

They were also discussing having the multi-line text control be deselected by default in future implementations of wxWidgets, and they had also mentioned that under OsX, that the default behavior was: single-line - auto-select all, multi-line - select none.

So the discrepancies in what you're seeing and what others are seeing may be due to different OS, or even different version of wxWidgets. Suffice to say, I have this issue too and haven't found the best work around.

HOWEVER, if you don't mind your multi-line text control processing tabs instead of the tabs being used for GUI navigation, use the attribute wxWANTS_CHARS and the selection problem will go away.

Mike Gibson
  • 342
  • 1
  • 3
  • 14