【问题标题】:How to place ListBox object at text cursor position如何将 ListBox 对象放置在文本光标位置
【发布时间】:2016-11-28 16:04:15
【问题描述】:

我有一个代码编辑器,它可以自动完成并给出如下图所示的代码提示

我能够创建一个自动补全单词列表,并且能够根据用户输入的内容进行过滤。我决定将它放在一个带有滚动条的 ListBox 小部件中。但是我怎样才能像下面的照片一样定位它

这是我的代码:

from tkinter import *
root=Tk()
text=Text(root)
text.pack()

...#Filtering part

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

root.mainloop()

【问题讨论】:

  • 也许使用menu 而不是listbox
  • 但是您将无法根据用户的需求显示特定的功能

标签: python text tkinter autocomplete listbox


【解决方案1】:

IDLE的AutoCompleteWindow / autocomplete_w (pre3.6 / current 3.6) 模块中的winconfig_event函数有这个代码。

    text = self.widget
    text.see(self.startindex)
    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
    else:
        # place acw above current line
        new_y -= acw_height
    acw.wm_geometry("+%d+%d" % (new_x, new_y))

我还没有真正阅读它,因为它做了它应该做的事情。它将弹出窗口的顶行放在光标行下方的行上,或将底行放在上面的行上。从您的图像中可以看出,您可能需要调整 x 位置。

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多