-1

can someone tell me how can I change color to certain parts of a tkinter text widget. This is a simple view of my code (it's a chat so it has to display who sent the message):

T = Text(window, bg="black", fg="white", font=("bold", 12))

def addMessage(text)
    T.insert("me: ")
    T.insert(text+"\n")

addMessage("hi")

how can I set the "me: " part to be blue and the text part to stay white?

Beyram
  • 11
  • 2
  • 1
    Does this answer your question? [how-to-change-the-color-of-certain-words-in-the-tkinter-text-widget](https://stackoverflow.com/questions/14786507) – stovfl May 10 '20 at 09:10
  • already seen, didn't understand much tho – Beyram May 10 '20 at 09:28
  • ***already seen,***: Read up on [How do I ask a good question? - Section: Search, and research - including links to related questions that haven't helped](https://stackoverflow.com/help/how-to-ask) – stovfl May 10 '20 at 09:58
  • The answer to your question can be found in standard documentation for the text widget, and there are countless examples on the internet. What specific problem are you having? – Bryan Oakley May 10 '20 at 13:49

1 Answers1

1

Try this:

from tkinter import *
window = Tk()

T = Text(window, bg="black", fg="white", font=("bold", 12))
# Create a tag named "blue" and set the color of it to blue
T.tag_config("blue", foreground="blue")
T.grid()

def addMessage(text):
    # apply the tag while inserting
    T.insert(END, "me: ", "blue")
    T.insert(END, text+"\n")

addMessage("hi")

window.mainloop()
ErdoganOnal
  • 800
  • 6
  • 13
  • 1
    You can simply pass the optional `tags` argument in `T.insert()`: `T.insert(END, "me: ", "blue")`. – acw1668 May 10 '20 at 09:14
  • @Beyram: how is adding `, "blue"` plus one additional line of code _complicated_? I don't think it's possible to make it any simpler. – Bryan Oakley May 10 '20 at 13:50
  • yes but it isn't obvious, in java the gui is pretty simple and obvious, you put all the configurations in one place without creating any additional tag or something – Beyram May 13 '20 at 07:51