Customtkinter brackets + color text?

Hey! I'm currently working with UIs in Python and came across customtkinter and the following problem: self.textbox.insert("0.0", f"(text1) text2 text3\n") I would like to color the brackets around text1 red and text2 green, but I can only change the entire text to one color. It would be nice if someone could help me.
LG

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

In tkinter itself, there is the option to assign text to a (or more) tag(s). One day can be connected to a specific style below.

Example:

text = tk.Text(root)
text.insert("end", "red text", "red")
text.insert("end", "blue text", "blue")

text.tag_configure("red", foreground="red")
text.tag_configure("blue", foreground="blue")

Here two texts (“red text”, “blue text”) are inserted, each one gets assigned one day (Red, blue). Via tag_configure determines which style is to be associated with a created day, which ultimately also affects the representation of the inserted text.

regex9
1 year ago
Reply to  einefragesir

This also works with customtkinter. There is the method (the CTkTextbox) to the day configuration tag_config.