wx textctrl font style

Discussion in 'Python' started by Pom, Mar 31, 2007.

  1. Pom

    Pom Guest

    Hello

    I have a tuple of strings which I must show in a textctrl, each item in
    the tuple representing one line of text.

    The first three lines of text should each have another style (fontsize
    and color)

    i'm using this code to achieve this:
    tmppos = self.txtInfo.GetInsertionPoint() # get the cursor pos
    self.txtInfo.write(str(csr[0])+'\n')
    tmppos2 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE",
    wx.NullColour, tmpnewfont))
    self.txtInfo.write(str(csr[1])+'\n')
    tmppos3 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    self.txtInfo.SetStyle(tmppos2, tmppos3, wx.TextAttr("BLUE",
    wx.NullColour, tmpnewfont2))
    self.txtInfo.write(str(csr[2])+'\n')
    tmppos4 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    self.txtInfo.SetStyle(tmppos3, tmppos4, wx.TextAttr(wx.Nullcolour,
    wx.NullColour, tmpnewfont3))


    Now, I was wondering if this is the right way of doing this, because I
    have the feeling this code sucks.

    Anybody has a better way of doing this?

    Thx !!!
     
    Pom, Mar 31, 2007
    #1
    1. Advertising

  2. Pom

    Guest

    On Mar 31, 3:13 am, Pom <> wrote:
    > Hello
    >
    > I have a tuple of strings which I must show in a textctrl, each item in
    > the tuple representing one line of text.
    >
    > The first three lines of text should each have another style (fontsize
    > and color)
    >
    > i'm using this code to achieve this:
    > tmppos = self.txtInfo.GetInsertionPoint() # get the cursor pos
    > self.txtInfo.write(str(csr[0])+'\n')
    > tmppos2 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    > self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE",
    > wx.NullColour, tmpnewfont))
    > self.txtInfo.write(str(csr[1])+'\n')
    > tmppos3 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    > self.txtInfo.SetStyle(tmppos2, tmppos3, wx.TextAttr("BLUE",
    > wx.NullColour, tmpnewfont2))
    > self.txtInfo.write(str(csr[2])+'\n')
    > tmppos4 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    > self.txtInfo.SetStyle(tmppos3, tmppos4, wx.TextAttr(wx.Nullcolour,
    > wx.NullColour, tmpnewfont3))
    >
    > Now, I was wondering if this is the right way of doing this, because I
    > have the feeling this code sucks.
    >
    > Anybody has a better way of doing this?
    >
    > Thx !!!


    You could experiment with the "Rich Text" style of the text control
    (see the wxPython Demo). You might also mess with refactoring your
    code a little and doing a FOR loop.

    # untested code!!!
    fonts = [tmpnewfont, tmpnewfont2, tmpnewfont3]
    count = 0
    tmppos = self.txtInfo.GetInsertionPoint()
    self.txtInfo.write(str(csr[count])+'\n')
    for i in range(3):
    tmppos2 = self.txtInfo.GetInsertionPoint()
    self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE",
    wx.NullColour, fonts[count]))
    tmppos = tmppos2
    count += 1
    self.txtInfo.write(str(csr[count])+'\n')


    I also noticed a FancyText widget. I've used the HtmlWindow widget for
    an About screen before and I thought it was pretty cool if you know
    HTML. You might also try dropping an email to the wxPython Users Group
    at http://wxpython.org/maillist.php

    Hope this helps!

    Mike
     
    , Apr 2, 2007
    #2
    1. Advertising

  3. Pom

    Pom Guest

    wrote:
    > On Mar 31, 3:13 am, Pom <> wrote:
    >> Hello
    >>
    >> I have a tuple of strings which I must show in a textctrl, each item in
    >> the tuple representing one line of text.
    >>
    >> The first three lines of text should each have another style (fontsize
    >> and color)
    >>
    >> i'm using this code to achieve this:
    >> tmppos = self.txtInfo.GetInsertionPoint() # get the cursor pos
    >> self.txtInfo.write(str(csr[0])+'\n')
    >> tmppos2 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    >> self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE",
    >> wx.NullColour, tmpnewfont))
    >> self.txtInfo.write(str(csr[1])+'\n')
    >> tmppos3 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    >> self.txtInfo.SetStyle(tmppos2, tmppos3, wx.TextAttr("BLUE",
    >> wx.NullColour, tmpnewfont2))
    >> self.txtInfo.write(str(csr[2])+'\n')
    >> tmppos4 = self.txtInfo.GetInsertionPoint() # get the new cursor pos
    >> self.txtInfo.SetStyle(tmppos3, tmppos4, wx.TextAttr(wx.Nullcolour,
    >> wx.NullColour, tmpnewfont3))
    >>
    >> Now, I was wondering if this is the right way of doing this, because I
    >> have the feeling this code sucks.
    >>
    >> Anybody has a better way of doing this?
    >>
    >> Thx !!!

    >
    > You could experiment with the "Rich Text" style of the text control
    > (see the wxPython Demo). You might also mess with refactoring your
    > code a little and doing a FOR loop.
    >
    > # untested code!!!
    > fonts = [tmpnewfont, tmpnewfont2, tmpnewfont3]
    > count = 0
    > tmppos = self.txtInfo.GetInsertionPoint()
    > self.txtInfo.write(str(csr[count])+'\n')
    > for i in range(3):
    > tmppos2 = self.txtInfo.GetInsertionPoint()
    > self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE",
    > wx.NullColour, fonts[count]))
    > tmppos = tmppos2
    > count += 1
    > self.txtInfo.write(str(csr[count])+'\n')
    >
    >
    > I also noticed a FancyText widget. I've used the HtmlWindow widget for
    > an About screen before and I thought it was pretty cool if you know
    > HTML. You might also try dropping an email to the wxPython Users Group
    > at http://wxpython.org/maillist.php
    >
    > Hope this helps!
    >
    > Mike
    >


    Thx alot !

    I know HTML, so I certainly will skip the richtext.

    I did'nt see that fancytext widget, I'll take a look and decide between
    that and the htmlwindow.

    I'm making a simple addressbook, which I can't find like I need it (with
    mysql db)


    Kindly regards
     
    Pom, Apr 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. newgene
    Replies:
    0
    Views:
    874
    newgene
    Jul 28, 2003
  2. Logan
    Replies:
    3
    Views:
    469
    Edward K. Ream
    Dec 2, 2003
  3. Hako
    Replies:
    6
    Views:
    3,767
    Frank Millman
    Jan 20, 2006
  4. Simon Hibbs

    TextCtrl focus events in wxWidgets

    Simon Hibbs, Jul 19, 2006, in forum: Python
    Replies:
    12
    Views:
    14,016
    Frank Millman
    Jul 19, 2006
  5. mttc
    Replies:
    2
    Views:
    2,463
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page