How to set custom color for symbols like*,#,etc in python tkinter

2024/11/16 9:33:04

How to set specific color for certain symbols like ,#,etc ' example if I type "" it's color should be blue and other stay remain same.

typedecker sir i am binding you function like this but this is not working


from tkinter import*
root = Tk()
def check_for_symbols(symbol_dict) :for i in symbol_dict :text.tag_remove(i, '1.0', END)pos = 1.0while 1:pos = text.search(i, pos, regexp = True, stopindex = END)if not pos:breaklast_pos = '%s+%dc' % (pos, len(i)) # The only changetext.tag_add(i, pos, last_pos)pos = last_postext.tag_config(i, foreground = symbol_dict[i])root.after(1000, lambda:check_for_symbols(symbol_dict))return
symbol_dict = {"*":"blue"
}
text = Text(root, background = "gray19", foreground = "white", insertbackground = 'white',font="Consolas 15 italic")
text.pack(expand=True,fill=BOTH)
root.after(1000, lambda : check_for_symbols(symbol_dict))
root.mainloop()
Answer

The same procedure as followed in the case of words here by me, can be followed, the only change will be in the regex expression that is to be used to detect the symbols.

last_pos = '%s+%dc' % (pos, len(i)) # The only change

The full check_for_symbols function with the necessary changes in place will look like -:

def check_for_symbols(symbol_dict) : # pass symbol dict as argument using lambda construct.
#    symbol dict format-: {keyword : color}for i in symbol_dict :text.tag_remove(i, '1.0', tk.END)pos = 1.0while 1:pos = text.search(i, pos, regexp = True, stopindex = tk.END)if not pos:breaklast_pos = '%s+%dc' % (pos, len(i)) # The only changetext.tag_add(i, pos, last_pos)pos = last_postext.tag_config(i, foreground = symbol_dict[i])root.after(1000, check_for_symbols)return
https://en.xdnf.cn/q/118945.html

Related Q&A

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …