简体   繁体   中英

How to read the text below the mouse when clicking on a tkinter Text widget and paste it in an Entry widget?

I am writing a program that pulls data from a server. Initially, the program asks the server to print the data available for a specific day. The output on the Text widget is shown in the for loop:

def set_id(event):
    entry1.delete(0, 'end')
    entry1.insert(0, "The text I have just clicked")

tex1.tag_config('clickable', foreground="blue", underline=True)
tex1.tag_bind('clickable', "<Button-1>", set_id)

for n in range(0, n_samples, 1):
    tex1.insert("end", str(json[n]['_id']) + "\n", 'clickable')
    # you get for example: 123\n121\n111 ecc
   

Later I want the user to be able to click on any of the printed samples id and paste that value to another Entry widget. Can it be done?

I hope I was able to explain myself, if not please ask for clarification. Thank you all for your help!

You can use:

tex1.get('insert linestart', 'insert lineend')

to get the ID where the input cursor is. However since you bind on <Button-1> , the input cursor is not updated when the callback is executed. You need to bind on <ButtonRelease-1> instead:

def set_id(event):
    entry1.delete(0, 'end')
    entry1.insert(0, tex1.get('insert linestart', 'insert lineend'))
...
tex1.tag_bind('clickable', "<ButtonRelease-1>", set_id) # bind on <ButtonRelease-1> instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM