satyam jha
satyam jha

Reputation: 23

underline only selected words in tkinter

I created a button underline by clicking this button the entire text inside the Entry gets underlined. But
I want only those words to be underlined which are selected by my cursor.

import tkinter as tk
from tkinter import font as tkFont
root = tk.Tk()


text = tk.Entry()
text.pack()

def underline():
    f = tkFont.Font(text, text.cget("font"))
    f.configure(underline = True)
    text.configure(font=f)


underline =tk.Button(root,text="underline",command=underline)
underline.pack()



root.mainloop()

Upvotes: 0

Views: 460

Answers (2)

Matiiss
Matiiss

Reputation: 6156

Here is a pretty easy way of doing that (it will also ununderline it if selection is already underlined) also can't do this with Entry:

from tkinter import Tk, Text, Button
from _tkinter import TclError


root = Tk()

text = Text(root)
text.pack()


def underline():
    try:
        if text.tag_nextrange('underline_selection', 'sel.first', 'sel.last') != ():
            text.tag_remove('underline_selection', 'sel.first', 'sel.last')
        else:
            text.tag_add('underline_selection', 'sel.first', 'sel.last')
            text.tag_configure('underline_selection', underline=True)
    except TclError:
        pass


underline_btn = Button(root, text="underline", command=underline)
underline_btn.pack()

root.mainloop()

So basically what happens is when calling the function it first looks if in the selection there already is an underlined tag (tag names can be what You want they are not predetermined by tkinter) and if it is clear the underline for the selection if not (so all the selected characters are not underlined) it applies underline.

try except is there to catch an error in case user presses a button without selection.

Upvotes: 1

Martin Wettstein
Martin Wettstein

Reputation: 2894

You can't underline certain words in an Entry widget. But you can underline some words in a Text widget by using tags. All you have to do is find the words that need to be underlined and then add them with tags. Probably the easiest way is to tokenize the whole entry and add it piecewise, underlining just the predefined words:

import tkinter as tk
from tkinter import font as tkFont
root = tk.Tk()

text = tk.Text(width=30,height=1,font=("Arial",11))
text.tag_configure("underlined",font=("Arial",11,"underline"))
text.pack()

def underline():
    words = ["hello","world"]
    content = text.get(1.0,tk.END+"-1c").split(' ')
    text.delete(1.0,tk.END)
    for c in content:
        if c in words:
            text.insert(tk.END,c, "underlined")
        else:
            text.insert(tk.END,c)
        text.insert(tk.END,' ')
    text.delete(tk.END+"-2c")
    
underline =tk.Button(root,text="underline",command=underline)
underline.pack()

root.mainloop()

The expression tk.END+"-1c" means that the last character (a line break) is omitted from the content. Likewise, the expression text.delete(tk.END+"-2c") removes the last two characters (a trailing blank and a line break).

Upvotes: 0

Related Questions