3

I try to implement a file manager, the question arises as using Treeview to display a directory for specified so that would have happened in TotalCommander'e

Here's a snippet of code:


from tkinter import *
from tkinter import ttk

class FileBrowser(Frame):

    def __init__(self, root):
        Frame.__init__(self, root)
        self.master.title('Файловый менеджер')
        self.master.geometry('1024x768')

        listframe = Frame(root, bd=5)
        listframe.pack(side='top', fill=BOTH, expand=1)

        self.filedirlist = ttk.Treeview(listframe)
        self.filedirlist["columns"]=("#1", "#2", "#3", "#4")
        self.filedirlist.column("#0", width=175, minwidth=100)
        self.filedirlist.column("#1", width=100, minwidth=100)
        self.filedirlist.column("#2", width=100, minwidth=100)
        self.filedirlist.column("#3", width=100, minwidth=100)
        self.filedirlist.column("#4", width=100, minwidth=100)
        self.filedirlist.heading("#0", text="Имя")
        self.filedirlist.heading("#1", text="Режим")
        self.filedirlist.heading("#2", text="Частота, МГц")
        self.filedirlist.heading("#3", text="Размер, Мб")
        self.filedirlist.heading("#4", text="Комментарий")
        self.filedirlist.pack(side='top', fill=BOTH, expand=1)
        listsb = Scrollbar(self.filedirlist,
                orient=HORIZONTAL, command=self.filedirlist.xview)
        listsb.pack(side=BOTTOM, fill=X)

        self.listbuttonopen = Button(listframe,
                text='Open').pack(side='left', padx='10', pady='5')
        self.listbuttondelete = Button(listframe,
                text='Delete').pack(side='left', padx='10', pady='5')
        self.listbuttonload = Button(listframe,
                text='Load').pack(side='left', padx='10', pady='5')

    if __name__=='__main__':
        root = Tk()
        FileBrowser(root).pack()
        root.mainloop()

When displaying folders should be active only the columns "Имя" and "Комментарий" and the remaining columns are active only when the contents of the selected folder.

Actually the question: Is it possible to implement it using Treeview'ra and how?)

P.S. Sorry for my English)

Paco
  • 4,520
  • 3
  • 29
  • 53
XFixer
  • 31
  • 2

1 Answers1

0

You can use the values keyword of tkinter.ttk.Treeview.insert in order to change specific columns of the table. You are already using the text option, but afterwards use values=("value1", "value2", "value3") in order to specify what the other columns display. For example:

Take your Treeview `filedirlist`:
----------------------------------------------
|   Name   |   Type   |   Size   |   Notes   |
----------------------------------------------
|                                            |
|                                            |
|                                            |
----------------------------------------------

filedirlist.insert("", tkinter.END, "element-id1", text="example1")
----------------------------------------------
|   Name   |   Type   |   Size   |   Notes   |
----------------------------------------------
| example1                                   |
|                                            |
|                                            |
----------------------------------------------

filedirlist.insert("", tkinter.END, "element-id2", text="example2", values=("type2", "size2", "notes2"))
----------------------------------------------
|   Name   |   Type   |   Size   |   Notes   |
----------------------------------------------
| example1                                   |
| example2   type2      size2      notes2    |
|                                            |
----------------------------------------------

Hope this helps!

noahbkim
  • 528
  • 2
  • 13