0

I am new to python. I use python 3.8 version. I try to iterate over the words and lines in the text box and need a way to highlight the words without highlighting the spaces. Since, the get() methods in tkinter takes index starting from 1.0 for 1st line, from 2.0 for 2nd line and so on.., i try to convert the original index with some calculations. But i cannot highlight all the text.

Thanks in advance!!

   from tkinter import *
    import tkinter

core = Tk()            
scroll = Scrollbar(core)
txt = Text(core, height = 35, width =85, yscrollcommand = scroll.set, \
           font =('bold',15),cursor="plus #aab1212" \
           )
# txt1.place(x=980, y=37)
scroll.config(command = txt.yview)
# scroll1.config(command = txt.xview)
scroll.pack(side=RIGHT, fill=Y)
txt.pack(side="right")

def get_index():
    l = []
    for i,w in enumerate(txt.get('1.0', 'end-1c').splitlines()):
        l.append(w)
        i = i + 1.1
        print(i,w)
        if i < 10:
            x = 1 + float(0) / 10
            txt.tag_add("start", x)
            txt.tag_config("start", background= "yellow", foreground= "black")
    print(l)
    for w,i in enumerate(l):
        print(i)
button1 = Button(text = 'index',command=get_index)
button1.place(x = 30, y = 50, height = 30, width = 150)
core.mainloop()

enter image description here

enter image description here

Kishan
  • 334
  • 2
  • 16
  • 1
    Indexes in Text widgets are not floats, and cannot be treated as such. As a simple example, `1.1` and `1.10` are equal as numbers, but nine characters apart as indexes. – jasonharper Jun 06 '23 at 14:55
  • The text widget has a `search` method that can return the index to a string within the text widget. – Bryan Oakley Jun 06 '23 at 16:52
  • This link might help to explain Text search. https://stackoverflow.com/questions/21507157/search-for-words-letters-in-the-text-widget-tkinter – Derek Jun 07 '23 at 03:08
  • @Derek, I am not trying to search the text in text box. I need to highlight all the words with some color in the text box excluding the spaces. – Kishan Jun 07 '23 at 06:07

1 Answers1

1

I try to iterate over the words and lines in the text box and need a way to highlight the words without highlighting the spaces.

The problem can be fixed by adapting my script. We will be using Walrus for Python 3.8 or later.

Using Python 3.12.0b3, Windows 10.

Snippet:

import  tkinter as tk

root = tk.Tk()
Frm = tk.Frame(root)

tk.Label(Frm,text='Enter Word to Find:').pack(side=tk.LEFT)
modify = tk.Entry(Frm)

modify.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
modify.focus_set()

buttn = tk.Button(Frm, text='Find')
buttn.pack(side=tk.RIGHT)
Frm.pack(side=tk.TOP)

txt = tk.Text(root, bg='white', fg='black')

txt.insert('1.0','''Highlight the words in this text box with yellow colour''')
txt.pack(side=tk.BOTTOM)

 
def find(event=None):
    
    txt.tag_remove('found', '1.0', tk.END)
    #ser = modify.get()
    if ser := modify.get():
        idx = '1.0'
        while 1:
            idx = txt.search(ser, idx, nocase=1, stopindex=tk.END)
            if not idx: break
            lastidx = '%s+%dc' % (idx, len(ser))
            
            txt.tag_add('found', idx, lastidx)
            idx = lastidx
        txt.tag_config('found', foreground='yellow')
    modify.focus_set()
buttn.config(command=find)
modify.bind("<Return>", find)

root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19