我是 Python 的新手,為了練習,我正在嘗試使用 tkinter 在 Text 小部件中創建一個串列框跟隨我的文本游標。(移動串列框基本上將用作自動完成工具。)這是最接近我的問題:是否可以將 tkinter 文本索引轉換為坐標?但不幸的是,對于帖子發布者的問題,有更好的解決方法,所以主要問題沒有得到回答。
我還嘗試使用 Text.window_create(),它使用索引來定位視窗,但視窗/小部件被視為文本,如果我按退格/洗掉,則會被洗掉。嘗試查看其內部代碼,但我無法理解:D
這是我的代碼:
def textChanged(event):
index = text1.index(tk.INSERT)
# coord = from index ??
# list1.place(coord[0],coord[1]
if __name__==‘__main__’:
root = Tk()
text1 = Text(root)
text1.place(x=0,y=0)
text1.bind(“<KeyRelease>“,textChanged)
list1 = Listbox(root)
謝謝!
uj5u.com熱心網友回復:
您可以使用text1.bbox(tk.INSERT)[:2]獲取text1插入游標的坐標(相對于 )。但是,由于list1是視窗的子root視窗,因此最好將此坐標偏移為text1內部root視窗的位置:
def textChanged(event):
# get the coordinates of text box inside root window
x0, y0 = text1.winfo_x(), text1.winfo_y()
# get the coordinates of the insertion cursor inside text box
x1, y1 = text1.bbox(INSERT)[:2]
# the coordinates of the insertion cursor relative to root window
# will be (x0 x1, y0 y1)
list1.place(x=x0 x1, y=y0 y1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490467.html
