Home>

Is it possible to change some of the data in Tkinter's Treeview?

For example, the code below

aaa, bbb, ccc


If there is

aaa, ddd, ccc


I want to do something like

After making a note of all the data, I thought of a way to erase all the lines, change some and redisplay.
UnderidIs it possible to change the data in the nth column using?

Thanks for your answer.

from tkinter import ttk
import tkinter as tk
if __name__ == "__main__":
    win = tk.Tk ()
    win.geometry ("500x400")
    # Get width
    return value of win.update_idletasks () # winfo_width () 1 Avoidance
    win_width = win.winfo_width ()
    # Stretch
    win.rowconfigure (0, weight = 1)
    win.columnconfigure (0, weight = 1)
    #Style_Theme
    s = ttk.Style ()
    s.theme_use ("clam")
    tree = ttk.Treeview (win)
    #Number of columns
    tree ["columns"] = (1, 2, 3)
    tree ["show"] = "headings"
    #Specify column width
    tree.column (1, width = int (win_width/3))
    tree.column (2, width = int (win_width/3))
    tree.column (3, width = int (win_width/3))
    #Header
    tree.heading (1, text = "Data1")
    tree.heading (2, text = "Data2")
    tree.heading (3, text = "Data3")
    tree.grid (row = 0, column = 0, sticky = "nsew")
    #Insert
    id = tree.insert ("", "end", value = ["aaa", "bbb", "ccc"])
    print (id)
    win.mainloop ()
  • Answer # 1

    If there is only one change

      tree.set (id, 2, "ddd")

    set (item, column = None, value = None) ¶

    When called with one argument, returns a dictionary of column/value pairs for the specified item.

    When called with two arguments, returns the current value of the specified column.

    When called with three arguments, sets the column of the given item to the specified value value.

    Note: column is not the number in column n tree ["columns"] = (1, 2, 3)Any name specified in. In this code, column name == number of columns, but if column name is a character The column name that specifies the nth column is tree ["columns"] [n-1]

    Correction: I was able to specify an integer.

    columns The name in the list of options.
    An integer n that points to the nth data column.
    A string of the form #n that points to the nth displayed column, where n is an integer.
    The option values ​​of the elements may appear in a different order than they are actually stored.
    Column # 0 always points to a tree column, even if show = "tree" is not specified.

  • Answer # 2

    For the time being, I will answer that it is such a thing

    If you take out the tuple, remake it, and put it back in as shown below, it will work like that.

      temp = tree.item (id, "values")
        temp = (temp [0], "ddd", temp [2])
        tree.item (id, values ​​= temp)

    However, it would be nice if there were 3 columns, but if there were more columns, it would be troublesome to remake the tuples, so use slices and expansions.

      temp = tree.item (id, "values")
        temp = (* temp [: 1], "ddd", * temp [2:])
        tree.item (id, values ​​= temp)

    It may be good to write.

    If you want to specify a specific line later, it is ↓.

      target = tree.get_children () [0] # 1st line
        temp = tree.item (target, "values")
        temp = (* temp [: 1], "ddd", * temp [2:])
        tree.item (target, values ​​= temp)