0

I'm looking for a way to copy a selected text in a tkinter.scrolledtext and paste it automatically in an Entry.

Means, Every time a user selects a text it will be added to the tkinter Entry automatically.

Any suggestions? specially on how to get the selected text.

1

1 Answer 1

2

You can use the selection event generated from text widget to add to your entry widget.

import tkinter as tk
from tkinter import scrolledtext as tkst

root = tk.Tk()

txt = tkst.ScrolledText(root)
txt.insert(tk.END, "This is a test phrase")
txt.pack()

entry = tk.Entry(root)
entry.pack()

def select_event(event):
    try:
        entry.delete(0, tk.END)
        entry.insert(0, txt.get(tk.SEL_FIRST, tk.SEL_LAST))
    except tk.TclError:
        pass

txt.bind("<<Selection>>", select_event)

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

I was looking exactly for this line: txt.bind("<<Selection>>", select_event) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.