Waheed Ahmad
Waheed Ahmad

Reputation: 60

tkinter Entry widget detect if there is any selection

Is there any way to detect selection in Entry widget in tkinter, like we get in Text widget txt.get(SEL_FIRST, SEL_LAST) and txt.tag_ranges("sel")

Upvotes: 1

Views: 730

Answers (1)

Art
Art

Reputation: 3089

If you just want to check if the selection is present you can use Entry.select_present() method which returns True if the selection is present.

If you want to get the selection you can use Entry.selection_get()

Minimal example:

from tkinter import *

def selection_present():
    print(entry.select_present())
    if entry.select_present():
        print(entry.selection_get())

root = Tk()

entry = Entry(root)
entry.pack()

Button(root, text="Check selection", command=selection_present).pack()

root.mainloop()

Upvotes: 2

Related Questions