Showing cursor in Text widget after selecting Listbox option
15:12 11 Jun 2025

The following Python code generates a list box and text widget. When I select an option, the cursor should show in the text widget but it does not. However, if I press Tab, the text widget is activated and then I can see the cursor.

import tkinter as tk

list_cursor_pos = ['2.5', '5.10', '8.15']

def on_listbox_select(event):
    i = listbox.curselection()[0]
    cursor_pos = list_cursor_pos[i]

    text_box.focus_set()
    text_box.mark_set("insert", cursor_pos)
    text_box.see(cursor_pos)
    text_box.event_generate('')
    
root = tk.Tk()

# Listbox
listbox = tk.Listbox(root)
listbox.pack(side = tk.LEFT, padx=10, pady=10)
for e in list_cursor_pos:
    listbox.insert('end', f'Go to {e} in the text')
listbox.bind("<>", on_listbox_select)

# Text box
text_box = tk.Text(root, height=15, width=50)
text_box.pack(side = tk.RIGHT, padx=10, pady=10)
# Create content for text box
content = ""
for i in range(9):
    content += f"{i+1}:234567890123456789\n"
text_box.insert("1.0", content)

root.mainloop()

When I do the same using a button instead of a list box, it works.

import tkinter as tk

def set_cursor_pos():
    cursor_pos = "2.5"
    text_box.focus_set()
    text_box.mark_set("insert", cursor_pos)

root = tk.Tk()

myButton = tk.Button(root, text = "Set cursor position at 2.5", command=set_cursor_pos)
myButton.pack(side = tk.LEFT, padx=10, pady=10)
myButton.focus_set()

# Text box
text_box = tk.Text(root, height=15, width=50)
text_box.pack(side = tk.RIGHT, padx=10, pady=10)
content = ""
for i in range(9):
    content += f"{i+1}:234567890123456789\n"
text_box.insert("1.0", content)

root.mainloop()
python tkinter listbox caret text-widget