954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

wxpython text colours

is it possible, well yes of course its possible, but does anyone know the best way to alter the colour of a word within a sentence in a wxpython window. I know how to change the colour of the entire text but not a specific word. So for instance if the word "danger" appears within the text it is always red? Any help greatly appreciated

juzzy
Newbie Poster
16 posts since Jan 2009
Reputation Points: 13
Solved Threads: 1
 

HTML code is easy to learn and you can format text using a simplified version of it, and show it in the wx.html.HtmlWindow() widget. Here is an example:
http://www.daniweb.com/forums/post803532-107.html

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

thanks ene for the reply

its not quite what im trying to do but handy to know nonetheless. Im wondering really how you would do it in a textctrl window using straight python (or if there is a wxpython method)?

juzzy
Newbie Poster
16 posts since Jan 2009
Reputation Points: 13
Solved Threads: 1
 

To my knowledge you cannot selectively color text with the simple wx.TextCtrl widget, for that you need the wx.StyledTextCtrl widget.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Oui, before I forget, take a look at the wx.RichTextCtrl example by Snee in the 'Starting wxPython' thread, that seems to be simpler for your purposes:
http://www.daniweb.com/forums/post763339-103.html

Here is a simple example:

# a simple sign using wx.richtext.RichTextCtrl()

import wx
import wx.richtext as rt

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        self.SetBackgroundColour("white")
        
        rich = rt.RichTextCtrl(self, wx.ID_ANY, value="")
        # default is black text
        rich.BeginFontSize(26)
        rich.BeginBold()
        rich.WriteText(" You are in ")
        rich.BeginTextColour('red')
        rich.WriteText("danger ")
        rich.EndTextColour()
        rich.WriteText("at that spot!")
        rich.EndBold()
        rich.EndFontSize()
        rich.WriteText("\n\n        The management")
        

app = wx.App()
mytitle = 'A wx.RichTextCtrl Sign'
width = 600
height = 140
# create the MyFrame instance and show the frame
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()
Attachments DangerSign.jpg 16.65KB
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

thats brilliant ene thank you, I think that could be exactly what i'm looking for

juzzy
Newbie Poster
16 posts since Jan 2009
Reputation Points: 13
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You