0

I am creating a code editor and want a side bar which contain files of folder open by user and how to open file by selecting file from Treeview in text Widget

I have create side for for file but still I got some problems that how to Open file from treeview in text widget

this is my code

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()
Coder
  • 92
  • 11
  • 1
    Try using `Treeview` instead of `Listbox`, like the answer in this [question](https://stackoverflow.com/questions/70763213/tkinter-treeview-how-to-add-a-icon-at-the-beginning-of-text) and this [question](https://stackoverflow.com/questions/68078498/recursively-arrange-all-folders-and-files-in-a-hierarchical-treeview-in-tkinter). – acw1668 Mar 16 '22 at 05:07
  • I tried Treeview but I am unable to insert all files of selected folder in Treeview with icons according to their extension and if it is folder then + is also put in front of icon – Coder Mar 16 '22 at 07:56
  • Post what you have tried on using `Treeview`, otherwise we cannot help. – acw1668 Mar 16 '22 at 08:25
  • Sir post my try above ,I have change my post please help me with my problem – Coder Mar 16 '22 at 13:01

1 Answers1

1

You just get the selected item iid inside Open_file_from_list_box():

def Open_file_from_list_box(value):
    global file
    file = tree.selection() # return item iid, not the file name
    ...

It should be:

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
    ...
acw1668
  • 40,144
  • 5
  • 22
  • 34