Change TextCtrl font

I think you need to be sure to apply the wx.TE_RICH or wx.TE_RICH2
style to the TextCtrl before you can set the font. At least, that's
what the wxPython demo does. Here's an example:

txt = wx.TextCtrl(parent, style=wx.TE_RICH2)

Hope that helps!

···

On Jul 14, 3:41 pm, Raoul <raoulpa...@yahoo.com> wrote:

Hi. I want to change just the font in a TextCtrl. Why does this have
no effect in MS Windows ? No errors are reported.

myTC = wx.TextCtrl( ... )

tcFont = myTc.GetFont() # OK
tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

myTC.SetFont( tcFont ) # ? works ?

{ write text to myTC ... } # text appears but in the original font
face

Thanks

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Above, you get the current font and call it tcFont. Later you SetFont() on the textCtrl using tcFont. So it will be the same font. In your middle step, you haven’t written tcFont, but tc.Font. You have ‘stored’ the original font in the name tcFont. I think that’s the problem.

Che

···

On Thu, Jul 15, 2010 at 9:45 AM, Mike Driscoll kyosohma@gmail.com wrote:

On Jul 14, 3:41 pm, Raoul raoulpa...@yahoo.com wrote:

Hi. I want to change just the font in a TextCtrl. Why does this have

no effect in MS Windows ? No errors are reported.

myTC = wx.TextCtrl( … )

tcFont = myTc.GetFont() # OK

tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

myTC.SetFont( tcFont ) # ? works ?

{ write text to myTC … } # text appears but in the original font

face

Mike - I am actually trying to modify your own example showing how to
redirect sys.stdout to a textCtrl. Adding "wx.TE_RICH" or
"wx.TE_RICH2" to the style has no effect.

C M - I made a transcription error. Here's the actual code.:

        wx.Frame.__init__( self, None, wx.ID_ANY, 'wxPython Redirect
Tutorial' )

        # Add a panel so it looks the same on all platforms
        panel = wx.Panel( self, wx.NewId() )

        #tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|
wx.HSCROLL| wx.TE_RICH
        tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|
wx.HSCROLL| wx.TE_RICH2

        logTC = wx.TextCtrl( panel, wx.NewId(), size=(300, 100),
style=tcStyle )

        # Set a monospace font
        tcFont = logTC.GetFont()
        tcFont.SetFamily( wx.FONTFAMILY_TELETYPE )
        logTC.SetFont( tcFont )

        ## This works, but it is not acceptable because
        ## I must know what are the last 2 arg values before the call.

···

##
        ##tcFontSize = tcFont.GetPointSize()
        ##logTC.SetFont( wx.Font( tcFontSize, wx.FONTFAMILY_TELETYPE,
wx.NORMAL, wx.NORMAL ) )

On Jul 15, 1:46 pm, C M <cmpyt...@gmail.com> wrote:

On Thu, Jul 15, 2010 at 9:45 AM, Mike Driscoll <kyoso...@gmail.com> wrote:

> On Jul 14, 3:41 pm, Raoul <raoulpa...@yahoo.com> wrote:
> > Hi. I want to change just the font in a TextCtrl. Why does this have
> > no effect in MS Windows ? No errors are reported.

> > myTC = wx.TextCtrl( ... )

> > tcFont = myTc.GetFont() # OK
> > tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

> > myTC.SetFont( tcFont ) # ? works ?

> > { write text to myTC ... } # text appears but in the original font
> > face

Above, you get the current font and call it tcFont. Later you SetFont() on
the textCtrl using tcFont. So it will be the same font. In your middle
step, you haven't written tcFont, but tc.Font. You have 'stored' the
original font in the name tcFont. I think that's the problem.

Che

The only "guaranteed correct" method I have found is to create a new
wx.Font from the settings of the old one:

#---- disassemble the original font & assemble a new one

# Ref: wx.Font( pointSize, family, style, weight, underline, face,
encoding )

fontPointSize = tcFont.GetPointSize()
# No need to get the family !
fontStyle = tcFont.GetStyle()
fontWeight = tcFont.GetWeight()
# To be thorough, [underline], [face] and [encoding] really should be
queried, too,
# but I'm lazy.

tcFont = wx.Font( fontPointSize, wx.FONTFAMILY_TELETYPE, fontStyle,
fontWeight )
logTC.SetFont( tcFont )

···

On Jul 16, 1:14 pm, WinCrazy <pas...@verizon.net> wrote:

Mike - I am actually trying to modify your own example showing how to
redirect sys.stdout to a textCtrl. Adding "wx.TE_RICH" or
"wx.TE_RICH2" to the style has no effect.

C M - I made a transcription error. Here's the actual code.:

    wx\.Frame\.\_\_init\_\_\( self, None, wx\.ID\_ANY, &#39;wxPython Redirect

Tutorial' )

    \# Add a panel so it looks the same on all platforms
    panel = wx\.Panel\( self, wx\.NewId\(\) \)

    \#tcStyle = wx\.TE\_MULTILINE|wx\.TE\_READONLY|wx\.VSCROLL|

wx.HSCROLL| wx.TE_RICH
tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|
wx.HSCROLL| wx.TE_RICH2

    logTC = wx\.TextCtrl\( panel, wx\.NewId\(\), size=\(300, 100\),

style=tcStyle )

    \# Set a monospace font
    tcFont = logTC\.GetFont\(\)
    tcFont\.SetFamily\( wx\.FONTFAMILY\_TELETYPE \)
    logTC\.SetFont\( tcFont \)

    \#\# This works, but it is not acceptable because
    \#\# I must know what are the last 2 arg values before the call\.
    \#\#
    \#\#tcFontSize = tcFont\.GetPointSize\(\)
    \#\#logTC\.SetFont\( wx\.Font\( tcFontSize, wx\.FONTFAMILY\_TELETYPE,

wx.NORMAL, wx.NORMAL ) )

On Jul 15, 1:46 pm, C M <cmpyt...@gmail.com> wrote:

> On Thu, Jul 15, 2010 at 9:45 AM, Mike Driscoll <kyoso...@gmail.com> wrote:

> > On Jul 14, 3:41 pm, Raoul <raoulpa...@yahoo.com> wrote:
> > > Hi. I want to change just the font in a TextCtrl. Why does this have
> > > no effect in MS Windows ? No errors are reported.

> > > myTC = wx.TextCtrl( ... )

> > > tcFont = myTc.GetFont() # OK
> > > tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

> > > myTC.SetFont( tcFont ) # ? works ?

> > > { write text to myTC ... } # text appears but in the original font
> > > face

> Above, you get the current font and call it tcFont. Later you SetFont() on
> the textCtrl using tcFont. So it will be the same font. In your middle
> step, you haven't written tcFont, but tc.Font. You have 'stored' the
> original font in the name tcFont. I think that's the problem.

> Che

Hi Ray, Che and Raoul,

redirect.py (2.09 KB)

···

On Fri, Jul 16, 2010 at 12:14 PM, WinCrazy pascor@verizon.net wrote:

Mike - I am actually trying to modify your own example showing how to

redirect sys.stdout to a textCtrl. Adding “wx.TE_RICH” or

“wx.TE_RICH2” to the style has no effect.

C M - I made a transcription error. Here’s the actual code.:

    wx.Frame.__init__( self, None, wx.ID_ANY, 'wxPython Redirect

Tutorial’ )

    # Add a panel so it looks the same on all platforms

    panel = wx.Panel( self, wx.NewId() )



    #tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|

wx.HSCROLL| wx.TE_RICH

    tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|

wx.HSCROLL| wx.TE_RICH2

    logTC = wx.TextCtrl( panel, wx.NewId(), size=(300, 100),

style=tcStyle )

    # Set a monospace font

    tcFont = logTC.GetFont()

    tcFont.SetFamily( wx.FONTFAMILY_TELETYPE )

    logTC.SetFont( tcFont )



    ## This works, but it is not acceptable because

    ## I must know what are the last 2 arg values before the call.

    ##

    ##tcFontSize = tcFont.GetPointSize()

    ##logTC.SetFont( wx.Font( tcFontSize, wx.FONTFAMILY_TELETYPE,

wx.NORMAL, wx.NORMAL ) )

On Jul 15, 1:46 pm, C M cmpyt...@gmail.com wrote:

On Thu, Jul 15, 2010 at 9:45 AM, Mike Driscoll kyoso...@gmail.com wrote:

On Jul 14, 3:41 pm, Raoul raoulpa...@yahoo.com wrote:

Hi. I want to change just the font in a TextCtrl. Why does this have

no effect in MS Windows ? No errors are reported.

myTC = wx.TextCtrl( … )

tcFont = myTc.GetFont() # OK

tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

myTC.SetFont( tcFont ) # ? works ?

{ write text to myTC … } # text appears but in the original font

face

Above, you get the current font and call it tcFont. Later you SetFont() on

the textCtrl using tcFont. So it will be the same font. In your middle

step, you haven’t written tcFont, but tc.Font. You have ‘stored’ the

original font in the name tcFont. I think that’s the problem.

Che

I just tried creating my own fun example. It works for me on Windows XP Pro, wxPython 2.8.10.1, Python 2.5. Let me know if it works for you guys!

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Mike - Yes it works fine, but we're "comparing apples to oranges"
here.
You create your various wx.Font_s from scratch, but what is wanted is
to take the existing font, whatever that happens to be at the moment,
and change JUST the type face and nothing else.

Ray Pasco

···

On Jul 16, 1:54 pm, Mike Driscoll <m...@pythonlibrary.org> wrote:

Hi Ray, Che and Raoul,

On Fri, Jul 16, 2010 at 12:14 PM, WinCrazy <pas...@verizon.net> wrote:
> Mike - I am actually trying to modify your own example showing how to
> redirect sys.stdout to a textCtrl. Adding "wx.TE_RICH" or
> "wx.TE_RICH2" to the style has no effect.

> C M - I made a transcription error. Here's the actual code.:

> wx.Frame.__init__( self, None, wx.ID_ANY, 'wxPython Redirect
> Tutorial' )

> # Add a panel so it looks the same on all platforms
> panel = wx.Panel( self, wx.NewId() )

> #tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|
> wx.HSCROLL| wx.TE_RICH
> tcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|
> wx.HSCROLL| wx.TE_RICH2

> logTC = wx.TextCtrl( panel, wx.NewId(), size=(300, 100),
> style=tcStyle )

> # Set a monospace font
> tcFont = logTC.GetFont()
> tcFont.SetFamily( wx.FONTFAMILY_TELETYPE )
> logTC.SetFont( tcFont )

> ## This works, but it is not acceptable because
> ## I must know what are the last 2 arg values before the call.
> ##
> ##tcFontSize = tcFont.GetPointSize()
> ##logTC.SetFont( wx.Font( tcFontSize, wx.FONTFAMILY_TELETYPE,
> wx.NORMAL, wx.NORMAL ) )

> On Jul 15, 1:46 pm, C M <cmpyt...@gmail.com> wrote:
> > On Thu, Jul 15, 2010 at 9:45 AM, Mike Driscoll <kyoso...@gmail.com> > > wrote:

> > > On Jul 14, 3:41 pm, Raoul <raoulpa...@yahoo.com> wrote:
> > > > Hi. I want to change just the font in a TextCtrl. Why does this have
> > > > no effect in MS Windows ? No errors are reported.

> > > > myTC = wx.TextCtrl( ... )

> > > > tcFont = myTc.GetFont() # OK
> > > > tc.Font.SetFamily( wx.FONTFAMILY_TELETYPE ) # ? works ?

> > > > myTC.SetFont( tcFont ) # ? works ?

> > > > { write text to myTC ... } # text appears but in the original font
> > > > face

> > Above, you get the current font and call it tcFont. Later you SetFont()
> on
> > the textCtrl using tcFont. So it will be the same font. In your middle
> > step, you haven't written tcFont, but tc.Font. You have 'stored' the
> > original font in the name tcFont. I think that's the problem.

> > Che

I just tried creating my own fun example. It works for me on Windows XP Pro,
wxPython 2.8.10.1, Python 2.5. Let me know if it works for you guys!

--
-----------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

redirect.py
2KViewDownload

The use of "font family" in Windows is problematic. The Windows font
picker uses a complicated heuristic to choose a real font based on the
parameters, and that heuristic is not always comprehensible by human
beings.

First, try wx.FONTFAMILY_MODERN instead of TELETYPE. If that doesn't
help, try specifying the actual typeface by name instead of by family.

···

On Jul 17, 10:50 am, WinCrazy <pas...@verizon.net> wrote:

Mike - Yes it works fine, but we're "comparing apples to oranges"
here.
You create your various wx.Font_s from scratch, but what is wanted is
to take the existing font, whatever that happens to be at the moment,
and change JUST the type face and nothing else.

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

Actually, I first tried wx.FONT_FAMILY_MODERN, but the its description
said that it would "probably" be a fixed pitch font (on every
platform). I want to guarantee that it IS a monospace font.
wx.FONT_FAMILY_TELETYPE must always be monospaced.

I can't specify the typeface by name because I don't known what that
name is on every platform. So, I must use the only guaranteed fixed
pitch font there is in wx, which is a generic font family.

···

On Jul 19, 1:52 pm, Tim Roberts <t...@probo.com> wrote:

On Jul 17, 10:50 am, WinCrazy <pas...@verizon.net> wrote:

> Mike - Yes it works fine, but we're "comparing apples to oranges"
> here.
> You create your various wx.Font_s from scratch, but what is wanted is
> to take the existing font, whatever that happens to be at the moment,
> and change JUST the type face and nothing else.

The use of "font family" in Windows is problematic. The Windows font
picker uses a complicated heuristic to choose a real font based on the
parameters, and that heuristic is not always comprehensible by human
beings.

First, try wx.FONTFAMILY_MODERN instead of TELETYPE. If that doesn't
help, try specifying the actual typeface by name instead of by family.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

I think you are asking for guarantees that cannot be made. wx is
thinly wrapping native GUI APIs. Each of the GUI systems on which wx
runs has its own unique, baroque, and largely inscrutable scheme for
converting generic "font attributes" into a specific typeface and
point size. There is no "guaranteed fixed pitch font".

If it were me, I'd ask for Courier by name. Otherwise, you can't
predict which typeface you get, and your application is not going to
have a predictable look.

···

On Jul 19, 4:26 pm, WinCrazy <pas...@verizon.net> wrote:

Actually, I first tried wx.FONT_FAMILY_MODERN, but the its description
said that it would "probably" be a fixed pitch font (on every
platform). I want to guarantee that it IS a monospace font.
wx.FONT_FAMILY_TELETYPE must always be monospaced.

I can't specify the typeface by name because I don't known what that
name is on every platform. So, I must use the only guaranteed fixed
pitch font there is in wx, which is a generic font family.