2

just a simple question on a wx.TextCtrl element. i have this text field where on an application, where the user can add a string on it. i want a text field with a red text on it. so i've generated this code:

self.hRepositoryTextfield = wx.TextCtrl(self.hPanel)
self.hRepositoryTextfield.SetDefaultStyle(wx.TextAttr(wx.RED))

if the user copy on this text field some string with applied font on it (for example a black coloured string, or a string with a particular font) the red color, anyway the default style is not respected. i would like the style i decide for my wx.TextCtrl is always forced according my settings. how can i do?

thank you in advance

axel

axel
  • 3,778
  • 4
  • 45
  • 72

3 Answers3

1

The SetForegroundColor might work on one OS and not on another. It depends on the native widget. I would set the TextCtrl's style flag to wx.TE_RICH. Check out the wxPython demo for an example. You can also use the StyledTextCtrl or FancyText of even the HTMLCtrl.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • +1 just cause Mike knows his wxPython better than just about anyone else on SO ... – Joran Beasley Dec 13 '12 at 16:58
  • i'm a newbie on python, so sorry for my question, but i don't understand @mike-driscoll what should i do with the flag wx.TE_RICH. `self.hRepositoryTextfield = wx.TextCtrl(self.hPanel, style=wx.TE_RICH) self.hRepositoryTextfield.SetDefaultStyle(wx.TextAttr(wx.RED))` are my new commands, but if i copy on my TextCtrl some text with other font or style, they won't be displayed in red as i would expect them to be. i will try with FancyText or your other suggestions. – axel Dec 13 '12 at 18:21
  • one more question. is there a way to write something like `self.hRepositoryTextfield.Bind(wx.EVT_????, self.setMyDefaultStyle)` because i don't understand if there's a Event for wx.TextCtrl changes – axel Dec 13 '12 at 19:33
  • 1
    The wxPython demo shows how to do it. When you create the text control you pass it the style=wx.TE_RICH. Then you use the SetStyle method to change a string of text to a different color. – Mike Driscoll Dec 13 '12 at 19:36
  • i didn't know the existence of wxPython Demo package... thanks a lot, if i will have any problem I'll tell you, otherwise, good programming to all. – axel Dec 14 '12 at 06:52
0
self.hRepositoryTextfield.SetForegroundColor(wx.RED)

that should work ....

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

i solved the problem in this way:

in the first part of the code it is defined my textfield style...

self.hRepositoryTextfield.SetStyle(0, len(self.hRepositoryTextfield.GetValue()), wx.TextAttr(wx.RED))
self.hRepositoryTextfield.SetFont(self.hFontLabel)
self.hRepositoryTextfield.Bind(wx.EVT_TEXT, self.forceDefaultStyle)

... then i bind every text change to my forcing-style function:

def forceDefaultStyle(self, event):
    hEventObject = event.GetEventObject()
    hEventObject.SetStyle(0, len(self.hRepositoryTextfield.GetValue()), wx.TextAttr(wx.RED))
    hEventObject.SetFont(self.hFontLabel)

and it works!

axel
  • 3,778
  • 4
  • 45
  • 72