Changing the foreground colour of a wx.TextCtrl does not work

Hi,

I am trying to change the colour of a wx.TextCtrl. ( Ubuntu 12.04 – Python 2.7.3 – wxPython Phoenix 3.0.3.dev1836+f764b32 gtk2 (phoenix) )

According to the TextCtrl class documentation, I am supposed to proceed like:

import wx

app = wx.App( 0 )
frame = wx.Frame( None )

tc = wx.TextCtrl( frame )
tc.SetDefaultStyle( wx.TextAttr( colText = wx.Colour( 255, 50, 100 ),
colBack = wx.Colour( 50, 255, 100 ) ) )
tc.AppendText( “Some red text” )

frame.Show( True )
app.MainLoop()

``

But it does not work. Any idea?

Thanks,

Damien

Did you try something like

import wx

app=wx.PySimpleApp()

frame=wx.Frame(None)

text=wx.StaticText(frame, label=“Colored text”)

text.SetForegroundColour((255,0,0)) # set text color

text.SetBackgroundColour((0,0,255)) # set text back color

frame.Show(True)

app.MainLoop()

···

On Thu, Apr 28, 2016 at 10:26 AM, Damien Ruiz ruizdams@gmail.com wrote:

Hi,

I am trying to change the colour of a wx.TextCtrl. ( Ubuntu 12.04 – Python 2.7.3 – wxPython Phoenix 3.0.3.dev1836+f764b32 gtk2 (phoenix) )

According to the TextCtrl class documentation, I am supposed to proceed like:

import wx

app = wx.App( 0 )
frame = wx.Frame( None )

tc = wx.TextCtrl( frame )
tc.SetDefaultStyle( wx.TextAttr( colText = wx.Colour( 255, 50, 100 ),
colBack = wx.Colour( 50, 255, 100 ) ) )
tc.AppendText( “Some red text” )

frame.Show( True )
app.MainLoop()

``

But it does not work. Any idea?

Thanks,

Damien

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Styles only work for multiline controls. You need TE_MULTILINE.
Otherwise, wx uses the simple native text control, which does not
support individual styling. You can change the overall foreground
and background color, as Emad notes, using other APIs.
And, if you are aiming at cross-platform, Emad is also correct that
styling on Windows requires the rich text control, TE_RICH.

···

Damien Ruiz wrote:

    I am trying to change the colour of a wx.TextCtrl. ( Ubuntu

12.04 – Python 2.7.3 – wxPython Phoenix 3.0.3.dev1836+f764b32
gtk2 (phoenix) )

    According to [          the

TextCtrl class documentation](http://wxpython.org/Phoenix/docs/html/TextCtrl.html#phoenix-title-textctrl-styles) , I am supposed to proceed
like:

import wx

            app = wx.App( 0 )

            frame = wx.Frame( None )



            tc = wx.TextCtrl( frame )

            tc.SetDefaultStyle( wx.TextAttr( colText

= wx.Colour( 255, 50, 100 ),\

                                             colBack = wx.Colour( 50, 255, 100 ) ) )

            tc.AppendText(                 "Some

red text" )

            frame.Show( True )

            app.MainLoop()

``

    But it does not work. Any idea?
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Thank you both.

As Tim said, using the following line works:
tc = wx.TextCtrl( frame, style = wx.TE_MULTILINE )

``

That point should be added to the documentation.

You are right too, Emad. The answer you took from that SO thread works:
tc.SetForegroundColour( (255, 0 , 0) )

``

I did not try it as I thought that function only exist for the wx.StaticText class… It actually comes from the wx.Window class, a common ancestor to wx.TextControl and wx.StaticText.

Damien

···

Le jeudi 28 avril 2016 13:58:32 UTC-4, Tim Roberts a écrit :

Damien Ruiz wrote:

    I am trying to change the colour of a wx.TextCtrl. ( Ubuntu

12.04 – Python 2.7.3 – wxPython Phoenix 3.0.3.dev1836+f764b32
gtk2 (phoenix) )

    According to [          the

TextCtrl class documentation](http://wxpython.org/Phoenix/docs/html/TextCtrl.html#phoenix-title-textctrl-styles) , I am supposed to proceed
like:

import wx

            app = wx.App( 0 )

            frame = wx.Frame( None )



            tc = wx.TextCtrl( frame )

            tc.SetDefaultStyle( wx.TextAttr( colText

= wx.Colour( 255, 50, 100 ),\

                                             colBack = wx.Colour( 50, 255, 100 ) ) )

            tc.AppendText(                 "Some

red text" )

            frame.Show( True )

            app.MainLoop()

``

    But it does not work. Any idea?
Styles only work for multiline controls.  You need TE_MULTILINE. 

Otherwise, wx uses the simple native text control, which does not
support individual styling. You can change the overall foreground
and background color, as Emad notes, using other APIs.

And, if you are aiming at cross-platform, Emad is also correct that

styling on Windows requires the rich text control, TE_RICH.

-- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

It IS in the documentation. Fourth section on that page:
TextCtrl Styles
Multi-line text controls support styling, i.e. provide a
possibility to set colours and font for individual characters in it (note that under Windows TE_RICH style is
requires for style support).

···

Damien Ruiz wrote:

Thank you both.

    As Tim said, using the following line works:

`tc = wx.TextCtrl( frame,
style = wx. TE_MULTILINE
)

              `

``

    That point should be added to [          the

documentation](http://wxpython.org/Phoenix/docs/html/TextCtrl.html#styles-window-styles).

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Oh, yes you are right.
I did not understand that you had to use the TE_MULTILINE flag by reading that sentence.

I was thinking about mentioning that style would not be applied in case the TE_MULTILINE flag is not used, in the Window styles section, third flag.

···

Le jeudi 28 avril 2016 14:57:03 UTC-4, Tim Roberts a écrit :

Damien Ruiz wrote:

Thank you both.

    As Tim said, using the following line works:

`tc = wx.TextCtrl( frame,
style = wx. TE_MULTILINE
)

              `

``

    That point should be added to [          the

documentation](http://wxpython.org/Phoenix/docs/html/TextCtrl.html#styles-window-styles).

It IS in the documentation.  Fourth section on that page:



    TextCtrl Styles



    Multi-line text controls support styling, i.e. provide a

possibility to set colours and font for individual

    characters in it (note that under Windows TE_RICH style is

requires for style support).

-- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.