Frage

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

War es hilfreich?

Lösung 3

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!

Andere Tipps

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.

self.hRepositoryTextfield.SetForegroundColor(wx.RED)

that should work ....

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top