Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of certain words in the tkinter text widget?

Tags:

I have a program that I want to be like the Python shell and change color of certain words when they are typed. Any help?

like image 338
Dan Alexander Avatar asked Feb 09 '13 09:02

Dan Alexander


People also ask

How do you change the color of text in Python GUI?

Python Tkinter Color ButtonButton text color can be changed by using keyword foreground or fg. Button background color can be changed by using keyword background or bg is used. Button color when clicked can be changed by using keyword activebackground and to change color of text use activeforeground.

Can you change the text of a label in tkinter?

Tkinter Label widgets are commonly used in applications to show text or images. You can change the label widget's text property, color, background, and foreground colors using different methods. You can update the text of the label widget using a button and a function if you need to tweak or change it dynamically.

How do I change text font in tkinter?

In your Python program, import tkinter. font as font, create font. Font() object with required options and assign the Font object to font option of Button.

How do you use custom colors in tkinter?

You can use a string specifying the proportion of red, green and blue in hexadecimal digits. For example, "#fff" is white, "#000000" is black, "#000fff000" is pure green, and "#00ffff" is pure cyan (green plus blue).


2 Answers

The main idea is to apply tags to the parts of text you want to customise. You can create your tags using the method tag_configure, with a specific style, and then you just need to apply this tag to the part of text you want to change using the method tag_add. You can also remove the tags using the method tag_remove.

The following is an example that uses tag_configure, tag_add and tag_remove methods.

#!/usr/bin/env python3  import tkinter as tk from tkinter.font import Font  class Pad(tk.Frame):      def __init__(self, parent, *args, **kwargs):         tk.Frame.__init__(self, parent, *args, **kwargs)          self.toolbar = tk.Frame(self, bg="#eee")         self.toolbar.pack(side="top", fill="x")          self.bold_btn = tk.Button(self.toolbar, text="Bold", command=self.make_bold)         self.bold_btn.pack(side="left")          self.clear_btn = tk.Button(self.toolbar, text="Clear", command=self.clear)         self.clear_btn.pack(side="left")          # Creates a bold font         self.bold_font = Font(family="Helvetica", size=14, weight="bold")          self.text = tk.Text(self)         self.text.insert("end", "Select part of text and then click 'Bold'...")         self.text.focus()         self.text.pack(fill="both", expand=True)          # configuring a tag called BOLD         self.text.tag_configure("BOLD", font=self.bold_font)      def make_bold(self):         # tk.TclError exception is raised if not text is selected         try:             self.text.tag_add("BOLD", "sel.first", "sel.last")                 except tk.TclError:             pass      def clear(self):         self.text.tag_remove("BOLD",  "1.0", 'end')   def demo():     root = tk.Tk()     Pad(root).pack(expand=1, fill="both")     root.mainloop()   if __name__ == "__main__":     demo() 

If you don't know what sel.first and sel.last are, check out this post or this reference.

like image 199
nbro Avatar answered Oct 08 '22 08:10

nbro


I have made a chat client. I highlighted certain parts of the conversation using a custom quite easy to use Text widget that allows you to apply tags using regular expressions. It was based on the following post: How to highlight text in a tkinter Text widget.

Here you have an example of use:

# "text" is a Tkinter Text  # configuring a tag with a certain style (font color) text.tag_configure("red", foreground="red")  # apply the tag "red"  text.highlight_pattern("word", "red") 
like image 27
Rnhmjoj Avatar answered Oct 08 '22 08:10

Rnhmjoj