如何在文本小部件 python tkinter 中从树视图打开文件

How to open file from treeview in text widget python tkinter

我正在创建一个代码编辑器并想要一个侧边栏,其中包含用户打开的文件夹文件以及如何通过从文本小部件中的 Treeview 选择文件来打开文件

我已经为文件创建了一面,但我仍然遇到一些问题,如何从文本小部件中的树视图打开文件

这是我的代码

from tkinter import*
from tkinter import ttk
import os
from tkinter.filedialog import askdirectory
def Open():
    for i in tree.get_children():
        tree.delete(i)
    path = askdirectory()
    abspath = os.path.abspath(path)
    root_node = tree.insert('', 'end', text=abspath, open=True)
    process_directory(root_node,abspath)
def process_directory( parent, path):

        for p in os.listdir(path):
            abspath = os.path.join(path, p)
            isdir = os.path.isdir(abspath)
            oid = tree.insert(parent, 'end', text=p, open=False)
            if isdir:
                process_directory(oid, abspath)
def Open_file_from_list_box(value):
    global file
    file = tree.selection()
    filepath = os.path.join(value,file)
    root.title(filepath + "                                                                                                                                            Code Editor")
    editor.delete(1.0,END)
    with open(filepath,"r") as f:
        editor.insert(1.0,f.read())
root = Tk()
root.geometry("1550x850+0+0")
Button(root,text="Open",command=Open).pack()
frame = Frame(root)
tree = ttk.Treeview(frame)
tree.pack(expand=True,fill=Y)
path = "."
tree.heading('#0', text=path, anchor='w')
abspath = os.path.abspath(path)
root_node = tree.insert('', 'end', text=abspath, open=True)
process_directory(root_node, abspath)
frame.pack(side=LEFT,fill=Y)
frame = Frame(root)
frame.pack(side=LEFT,expand=True,fill=BOTH)
editor = Text(frame,font="Consolas 15")
editor.pack(expand=True,fill=BOTH)

tree.bind("<<TreeviewSelect>>",lambda event=None:Open_file_from_list_box(path))

root.mainloop()

您刚刚在 Open_file_from_list_box():

中获取所选项目 iid
def Open_file_from_list_box(value):
    global file
    file = tree.selection() # return item iid, not the file name
    ...

应该是:

def Open_file_from_list_box(value):
    global file
    item_id = tree.selection()
    file = tree.item(item_id, 'text') # get the filename from 'text' option
    ...