linyicongvince
linyicongvince

Reputation: 11

How to correctly save a absolute path as the values of a treeview?

I want to save an absolute path as the values of a treeview of tkinter. But when I print the values, I found that it wasn't a standard absolute path.

I need to use the path in other part of code. And the only way I could figure out is to save it as the values of a treeview.

import tkinter
import os
from tkinter import ttk

path = r"D:\Documents\Desktop\Project"

class TreeWindows(tkinter.Frame):
    def __init__(self, master, path):
        frame = tkinter.Frame(master)
        frame.pack()

        self.tree = ttk.Treeview(frame)
        self.tree.pack()

        root = self.tree.insert("", "end", text=path, values=path)
        print(self.tree.item(root)["values"][0])
        # The result of printing is "D:DocumentsDesktopProject", but what I expect is "D:\Documents\Desktop\Project".

The result of printing is "D:DocumentsDesktopProject", but what I expect is "D:\Documents\Desktop\Project".

Upvotes: 1

Views: 149

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386020

The values parameter needs to be a list. You're giving it a string. The insert statement should look like this:

root = self.tree.insert("", "end", text=path, values=(path,))

Upvotes: 1

Related Questions