Home python Python Tkinter Color text

Python Tkinter Color text

Author

Date

Category

I want to implement a color conclusion as in the picture, only not in the console, but in Tkiter Label.
Is there such an opportunity? If so, how?

from tkinter import *
Window = TK ()
LBL = Label (Window, Text = 'Normal Text \ NRED TEXT')
lbl.pack ()
Window.Mainloop ()

In the picture, an example from the colorama


Answer 1

Widget Text allows you to display the text in different colors:

import tkinter as tk
root = tk.tk ()
Text = TK.Text ()
text.pack ()
# For tag 'tag_red_text' set red text
text.tag_config ('tag_red_text', foreground = 'red')
text.insert (tk.end, 'Normal Text \ N') # add ordinary text
text.insert (tk.end, 'Red Text', 'Tag_Red_Text') # add text with tag
text.config (state = tk.disabled) # Making a text field is irreducted (not required)
root.mainloop ()

Answer 2, Authority 100%

just add to Label – FG

root = label (text = 'test', fg = "green")

If the back background is needed, then simply use instead of FG – BG

Full example:

from tkinter import *
Window = TK ()
LBL = Label (Window, Text = 'Normal Text')
lbl.pack ()
LB = Label (Window, Text = 'Red Text', FG = 'Red')
lb.pack ()
Window.Mainloop ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions