0

I am making a text editor in tkinter for code, and I want to loop through every single word in the ScrolledText, and then change the color of it depending which word it is, but how?

I've already searched it up and found nothing that helped me. I also tried it on myself but I didn't know how to loop through the words forever (since while True made the program stop responding) and I don't know how to change the text color for only one bit.

This is all the code of my editor, some variables are defined in different functions:

def bie_save():
    bie_file_read = open(bie_file,"r")
    contents = bie_file_read.read()
    bie_file_read.close()
    data = text_area.get("1.0",tk.END + "-1c")
    bie_file_file = open(bie_file,"w")
    bie_file_file.write(data)
    bie_file_file.close()

def builtin_editor(file,protype,proname):
    global bie_file
    bie_file = file

    bie = tk.Tk()
    bie.title("FIDE - Editor")
    bie.geometry("800x450")

    global text_area
    text_area = st.ScrolledText(bie,width=800,height=450)
    text_area.pack()

    bie_file_read = open(bie_file,"r")
    contents = bie_file_read.read()
    bie_file_read.close()
    text_area.insert("1.0",contents)

    menu = tk.Menu(bie)
    bie.config(bg=bg_theme,menu=menu)
    fileMenu = tk.Menu(menu)
    menu.add_cascade(label="File",menu=fileMenu)
    fileMenu.add_command(label="Save",command=bie_save)
    fileMenu.add_separator()
    fileMenu.add_command(label="Exit")

    bie.mainloop()

I want it like when you type "pass" it changes to orange or something like that, but if you type something else it won't.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
wotman
  • 45
  • 1
  • 2
  • 9
  • 1
    This appears to be a duplicate of https://stackoverflow.com/q/3781670/7432 – Bryan Oakley Jul 26 '19 at 17:41
  • I don't understand what this means: _"I didn't know how to loop through the words forever "_ - why do you need to loop forever? Is what you're really asking is how to do the highlighting as the user types? Or, are you looking for a function that can search the entire document once when you call the function? – Bryan Oakley Jul 26 '19 at 17:44
  • @BryanOakley I never can know cause when I run that I get alot of errors about variables not being defined. – wotman Jul 26 '19 at 17:47
  • @BryanOakley with forever I mean it looping through all the words without ever stopping – wotman Jul 26 '19 at 17:48
  • 2
    Why do you want it to never stop? The user won't be able to enter text while it's looping, and once it's processed all the words there's no point in continuing to highlight the same word over and over. – Bryan Oakley Jul 26 '19 at 17:53
  • @BryanOakley but then he types another word and it doesn't change its color – wotman Jul 26 '19 at 17:54
  • Ok, so you're not asking it to run forever, you're asking for highlighting to be applied while someone is typing. – Bryan Oakley Jul 26 '19 at 18:01
  • You need to [edit] your question and add a definition of `st.ScrolledText`. – martineau Jul 26 '19 at 18:35
  • at start you have to check all words once so it doesn't mean looping forever. Later when user types new key then you can check only new word and highlight it or unhighlight - eventually you can check all words again but it doesn't mean looping forever too. – furas Jul 27 '19 at 08:10
  • @furas great idea! That helped me instantly! – wotman Jul 28 '19 at 14:54
  • If you want to do a code editor, you might be interested in these questions about syntax highlighting in tkinter text widget with [pygments](https://pygments.org/): https://stackoverflow.com/questions/29688831/pygments-syntax-highlighter-in-python-tkinter-text-widget, https://stackoverflow.com/questions/65192363/markdown-text-highlighting-performance-issues-tkinter/65200005#65200005, https://stackoverflow.com/questions/29688831/pygments-syntax-highlighter-in-python-tkinter-text-widget – j_4321 Jul 22 '22 at 14:46

0 Answers0