PySimpleGUI-Projects icon indicating copy to clipboard operation
PySimpleGUI-Projects copied to clipboard

Suggestion: Populating Listbox with the folder and file

Open shrivatsahosabettu opened this issue 2 years ago • 3 comments

I am following the code of yours to add images to list box, but before even trying that I want to convert the files and folders from the path to the list box. Not sure how to go ahead on this one.

Do I need to populate the data list first before even populating the tree as the treedata is converting to list box. or do I need to change my path to class structure?

I opened this here as the example which I am following is in your repository. Please let me know if I need to open this in PySimpleGUI Github

import PySimpleGUI as sg
import getpass, os, sys

default_path = os.path.join('C:\\', 'Users', getpass.getuser())
sg.set_options(font=('Roboto', 10))
back_path_holder = []
forward_path_holder = []
sg.theme('Dark Purple 4')


def list_files_and_folders(event, values, selected_folder):
    file_list = os.listdir(selected_folder)
    window['-TREE-'].update(file_list)
    print(file_list)


def prepare_header(data):
    for files_or_folders in data:
        treedata.Insert('', files_or_folders, files_or_folders, values=[], )


def common_functions(event, values):
    if back_path_holder:
        window['-BACK-'].update(disabled=False)
    else:
        window['-BACK-'].update(disabled=True)

    if forward_path_holder:
        window['-FORWARD-'].update(disabled=False)
    else:
        window['-FORWARD-'].update(disabled=True)


headings = ['Files or Folders', 'Status']

treedata = sg.TreeData()

list_element = [sg.Tree(data=treedata, headings=headings[1:], auto_size_columns=True,
                        num_rows=10, key='-TREE-', row_height=48,
                        metadata=[],
                        col0_width=70,
                        show_expanded=False, enable_events=True,
                        select_mode=sg.TABLE_SELECT_MODE_BROWSE)]

left_elements = [
    [sg.B('Up', k='-UP-', expand_x=True), sg.B('Back', k='-BACK-', expand_x=True, disabled=True),
     sg.B('Forward', k='-FORWARD-', expand_x=True, disabled=True),
     sg.InputText(k='-INPUT-', size=(30, 1), expand_x=True),
     sg.B('Browse', k='-BROWSE-', initial_folder=default_path)],
    list_element,
    # [sg.Listbox(values=[], k='-LIST-', size=(80, 20), expand_x=True, expand_y=True)],
]
right_elements = [
    [sg.T('test', key='-FFINFO-', size=(20, 10), expand_y=True, text_color='white')],
    [sg.T('test2', key='-FFIMAGE-', size=(20, 10), expand_y=True, text_color='white')],
]

layout = [
    [sg.Column(left_elements, expand_y=True, expand_x=True),
     sg.Column(right_elements, expand_y=True, expand_x=True)]]

# sg.main_sdk_help()

window = sg.Window("FF Tracker", layout, resizable=True, size=(800, 600), finalize=True)
tree = window['-TREE-']
tree.Widget.heading('#0', text='Files or Folders')
while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    common_functions(event, values)

    if event == '-BROWSE-':
        if values['-INPUT-']:
            selected_folder = sg.popup_get_folder('Select Folder', initial_folder=values['-INPUT-'],
                                                  no_window=True)
        else:
            selected_folder = sg.popup_get_folder('Select Folder', initial_folder=default_path, no_window=True)
        window['-INPUT-'].update(selected_folder)
        list_files_and_folders(event, values, selected_folder)
    if event == '-UP-':
        current_working_dir = values['-INPUT-']
        back_path_holder.append(current_working_dir)
        window['-BACK-'].update(disabled=False)
        up_dir = os.path.normpath(current_working_dir + os.sep + os.pardir)
        window['-INPUT-'].update(up_dir)
        list_files_and_folders(event, values, up_dir)
    if event == '-BACK-':
        if back_path_holder:
            window['-BACK-'].update(disabled=False)
            current_working_dir = back_path_holder.pop()
            forward_path_holder.append(current_working_dir)
            window['-FORWARD-'].update(disabled=False)
            window['-INPUT-'].update(current_working_dir)
            list_files_and_folders(event, values, current_working_dir)
        else:
            window['-BACK-'].update(disabled=True)
    if event == '-FORWARD-':
        if forward_path_holder:
            window['-FORWARD-'].update(disabled=False)
            current_working_dir = forward_path_holder.pop()
            back_path_holder.append(current_working_dir)
            window['-BACK-'].update(disabled=False)
            window['-INPUT-'].update(current_working_dir)
            list_files_and_folders(event, values, current_working_dir)
        else:
            window['-FORWARD-'].update(disabled=True)

window.close()

shrivatsahosabettu avatar Jun 21 '23 12:06 shrivatsahosabettu

Not sure the question is, but

if you use the sg.Tree element, you have to convert your list into sg.TreeData firstly, then update the sg.Tree element, like

def list_files_and_folders(event, values, selected_folder):
    file_list = os.listdir(selected_folder)
    treedata = sg.TreeData()
    for i, file in enumerate(file_list):
        treedata.insert("", i, file, [] )
    window['-TREE-'].update(treedata)

or, if you use the sg,Listbox element, just update the sg.Listbox with your one-dimension file list.

def list_files_and_folders(event, values, selected_folder):
    file_list = os.listdir(selected_folder)
    window['-LISTBOX-'].update(file_list)

jason990420 avatar Jun 21 '23 13:06 jason990420

Not sure the question is, but

if you use the sg.Tree element, you have to convert your list into sg.TreeData firstly, then update the sg.Tree element, like

def list_files_and_folders(event, values, selected_folder):
    file_list = os.listdir(selected_folder)
    treedata = sg.TreeData()
    for i, file in enumerate(file_list):
        treedata.insert("", i, file, [] )
    window['-TREE-'].update(treedata)

or, if you use the sg,Listbox element, just update the sg.Listbox with your one-dimension file list.

def list_files_and_folders(event, values, selected_folder):
    file_list = os.listdir(selected_folder)
    window['-LISTBOX-'].update(file_list)

I think the first example suits to me. I am using the Tree. My project is about a status tracker of the files and folders. Where you can mark the status of the file/folder with the image as update in the list box saying completed, important, Remove, Need to see, etc.. These information I am planning to store in a file with Filepath as dictory key and file and inner dictionay key and these status as values. Basically dictionary of dictionary in the file. Hope this is not a complex one which cannot be done using PySimpleGUI.

shrivatsahosabettu avatar Jun 21 '23 16:06 shrivatsahosabettu

Maybe you can refer this: https://github.com/jason990420/PySimpleGUI-Solution/issues/96 image

headings #0 is for the icon and text of heading #0, and you can put the filename and its status to values list when you insert each item into the treedata.

heading #0 or icon cannot be placed at other column and it is defined by tkinter.

treedata.insert(parent, key, text, values, icon=None)

Inserts a node into the tree. This is how user builds their tree, by Inserting Nodes This is the ONLY user callable method in the TreeData class

:param parent: the parent Node :type parent: (Node) :param key: Used to uniquely identify this node :type key: str | int | tuple | object :param text: The text that is displayed at this node's location :type text: (str) :param values: The list of values that are displayed at this node :type values: List[Any] :param icon: icon :type icon: str | bytes

jason990420 avatar Jun 21 '23 20:06 jason990420