Hung Truong Hung Truong - 2 months ago 8x
Python Question

How to place ListBox object at text cursor position

I have a code editor, that can autocomplete and give hints of code like the picture below

Example picture

I was able to create a list of Autocompletion words and was able to filter down accordingly to what the user types in. I decided to put it in a ListBox widget, with a scroll bar. But how can I position it like the photo below

Here's my code:

from tkinter import *

...#Filtering part

l= Listbox(root,listvariable=finalList )#The list after being filtered
...#How to position it like the picture



The winconfig_event function in IDLE's AutoCompleteWindow / autocomplete_w (pre3.6 / current 3.6) module has this code.

    text = self.widget
    x, y, cx, cy = text.bbox(self.startindex)
    acw = self.autocompletewindow
    acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
    text_width, text_height = text.winfo_width(), text.winfo_height()
    new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
    new_y = text.winfo_rooty() + y
    if (text_height - (y + cy) >= acw_height # enough height below
        or y < acw_height): # not enough height above
        # place acw below current line
        new_y += cy
        # place acw above current line
        new_y -= acw_height
    acw.wm_geometry("+%d+%d" % (new_x, new_y))

I have not really read it, because it does what it is supposed to do. It puts the top line of the popup on the line below the cursor line or the bottom line on the line above. It appears from your image that you might want to adjust the x position.