Is there a way to catch the text a user types, then do some function on it and then return it to the screen?

input(text) -> function(text) -> output(text)

I want to colourize certain words, so I want the function to find those words and then colorize them..

Here is one way to do this ...

# 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, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")
        
        rich = rt.RichTextCtrl(self, -1, 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(0)
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()

Well I already found that code snippet ;),
my problem is that I don't know what will be entered,
in your case you chose to print "you are in danger"
in my case the text depends on the user,
that's why I want to catch the input before it's outputted...

You can do all of that with standard Python code. Break up your text into a bunch of separate strings, then apply to the richtext widget.

You can do all of that with standard Python code.

Thanks to point that out, but could you also say how?

I make that a homework assignment.

Well what I would do now is:
-on key down event: capture all text in richtextCtrl
-pass that to a function
-re-output it,
this will take though long when you have a large file :s

Is this totally the wrong way?
or am I on a good track?

Nah that is very very simple. See you get the text with the GetValue() method. If you look at the link i posted you will see that on that page it shows that the return value for the GetValue() function is a string.. sounds like what you want eh?

Next, you can give that string to any function you like, thats obvious to do.

And to put it onto the richtextctrl you will need another method, now logically think, if there was a GetValue() method, then there is probably a SetValue( value ) method, and voila! There is. All you need to do is give the method an argument of the string that you want to set the value to and you are done.

Hope that helps

What you are trying to do is called syntax highlighting.

What you are trying to do is called syntax highlighting.

That's indeed what I want to do...

Nah that is very very simple. See you get the text with the GetValue() method. If you look at the link i posted you will see that on that page it shows that the return value for the GetValue() function is a string.. sounds like what you want eh?

Next, you can give that string to any function you like, thats obvious to do.

And to put it onto the richtextctrl you will need another method, now logically think, if there was a GetValue() method, then there is probably a SetValue( value ) method, and voila! There is. All you need to do is give the method an argument of the string that you want to set the value to and you are done.

Hope that helps

lol, thanks for your help, but I already knew about the Get and Set things, I have used wxPython for a while now.

The only thing I was not sure about was if this was a good way or not.

This is the current function, I have commented the things that could be not clear (although I think everything should be)

The problem is that if I press a sentence , space , in
from the moment I press the n from "in", the "in" becomes red for a second and gets cut out of the sentence and is being replaced in front of the sentence..
so: test in -> intest

My question is, why is the in replaced? if I use other words, the spaces are correct

def OnKeyUp(self, event):
        insp=self.text_ctrl_1.GetInsertionPoint()
        words=self.text_ctrl_1.GetValue().split(" ") #get all the words
        hl=[["in","red"]]  #syntax list All words=in should become red
        prev="" #previous words processed
        wordcount=0
        for word in words:
            for lh in hl:
                if lh[0]==word:  #if word=="in"
                    if wordcount>0:  #if word is not first word
                        self.text_ctrl_1.SetValue(prev+" ")   #print all previous text
                        self.text_ctrl_1.BeginTextColour('red')
                        self.text_ctrl_1.WriteText(word)  #print "in" in red
                        self.text_ctrl_1.EndTextColour()
                        prev+=" "+word
                    elif wordcount==0:  #if word is first word (You can Ignore this part)
                        self.text_ctrl_1.SetValue(word)
                        prev=word
                else:  #if word!="in"
                    if wordcount>0: #if word is not first word
                        self.text_ctrl_1.SetValue(prev+" "+word) #print all previous words + space + word
                        prev+=" "+word
                    elif wordcount==0: #if word is first word
                        self.text_ctrl_1.SetValue(word) #print word
                        prev=word
            wordcount+=1
        self.text_ctrl_1.SetInsertionPoint(insp)
        event.Skip()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.