Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multicolour text with ScrolledText widget?

from tkinter import *
from tkinter.scrolledtext import ScrolledText

window= Tk()
window.geometry('970x45')
box = ScrolledText(window, width=70, height=7).pack()
box.insert(END, "Ehila") #this insert "Ehila" into the box
box.congif(foreground='green') #this change the colour of "Ehila" into green colour
box.insert(END, "Now") #this insert "Now" into the box
box.congif(foreground='red') #this change the colour of "Now" into red colour but also "Ehila" become red and I don't want this!

I would like to colour each text colored with a different colour but I don't obtain at the end this result. How can I keep the colour of each insertion?

like image 750
lausent Avatar asked Jul 02 '16 11:07

lausent


1 Answers

Insert text with tags (insert method accept optional tag parameter(s)). Later use Text.tag_config to change the color of texts which tagged.

from tkinter import *
from tkinter.scrolledtext import ScrolledText

window = Tk()
window.geometry('970x45')
box = ScrolledText(window, width=70, height=7)
box.pack()
box.insert(END, "Ehila", 'name')  # <-- tagging `name`
box.insert(END, "Now", 'time')  # <-- tagging `time`
box.tag_config('name', foreground='green')  # <-- Change colors of texts tagged `name`
box.tag_config('time', foreground='red')  # <--  Change colors of texts tagged `time`

window.mainloop()
like image 160
falsetru Avatar answered Sep 21 '22 10:09

falsetru