Treeview是Tkinter8.5新引入的控件,可以用于实现较为复杂的GUI界面。本文使用一个FileBrowser实例来说明它的用法。
构建主窗口和退出菜单的代码和前一篇文章几乎相同:
root = Tk()root.title('Tkinter Treeview and PanedWindow Demo V1.0')root.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0, weight=1)screen_w = root.winfo_screenwidth()screen_h = root.winfo_screenheight()root.maxsize(width=screen_w, height=screen_h)rw = int(screen_w / 2)rh = int(screen_h / 2)root.geometry('{}x{}+{:g}+{:g}'.format(rw, rh, rw / 2, rh / 2))top_menu = Menu(root)root.config(menu=top_menu)file_menu = Menu(top_menu, tearoff=False)top_menu.add_cascade(label='File', menu=file_menu)file_menu.add_command(label="Exit", command=root.quit)
构建一个水平方向的PanedWindow控件,grid布局时sticky属性设置为四面填充。
paned_window = PanedWindow(root, orient=HORIZONTAL)paned_window.grid(row=0, column=0, sticky='eswn')
构建管理左侧控件的Frame对象:
tree_area = Frame(paned_window)tree_area.grid_rowconfigure(0, weight=1)tree_area.grid_columnconfigure(0, weight=1)paned_window.add(tree_area)
准备Treeview图标文件:
cur_path = os.path.abspath(os.path.dirname(__file_