Python 3. TTK. How to change value of the specific cell?
Question:
I have some problem with tkinter/ttk
.
So, i know how to get Treeview.focus, but how to change value of the specific cell in this table? Any suggestions?
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1","SLOT_2")
tview.column("SLOT_1", width=100 )
tview.column("SLOT_2", width=100)
tview.heading("#0",text="Column 0",anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def add_item():
tview.insert("","end",values=("","bar"))
def edit_item():
focused = tview.focus()
print(tview.item(focused))
tview.pack()
add_item = tk.Button(root,text="Add item",command=add_item)
add_item.pack(expand=True,fill='both')
edit_item = tk.Button(root,text="Edit item",command=edit_item)
edit_item.pack(expand=True,fill='both')
root.mainloop()
I’m using Python 3.6
with tkinter/ttk
.
Answers:
I added a thread so the program doesn’t hang while it waits for the user to enter the edit. You’ll probably want to add a text box or multiple text boxes for the edits to be entered
import tkinter as tk
import tkinter.ttk as ttk
import threading
root = tk.Tk()
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1", "SLOT_2")
tview.column("SLOT_1", width=100)
tview.column("SLOT_2", width=100)
tview.heading("#0", text="Column 0", anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def test_program_thread():
thread = threading.Thread(None, edit_item, None, (), {})
thread.start()
def add_item():
tview.insert("", "end", values=("", "bar"))
def edit_item():
focused = tview.focus()
x = input('Enter a Value you want to change')
tview.insert("", str(focused)[1:], values=("", str(x)))
tview.delete(focused)
tview.pack()
add_item = tk.Button(root, text="Add item", command=add_item)
add_item.pack(expand=True, fill='both')
edit_item_button = tk.Button(root, text="Edit item", command=test_program_thread)
edit_item_button.pack(expand=True, fill='both')
root.mainloop()
Building on Gardener85’s answer.
Using a single .item to update the line instead of adding a new line with .insert and then deleting the old line with .delete will help with a Treeview that contains more than 9 lines and keep the updated line in the same position in the Treeview.
Full code for modified Gardener85’s answer:
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1", "SLOT_2")
tview.column("SLOT_1", width=100)
tview.column("SLOT_2", width=100)
tview.heading("#0", text="Column 0", anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def test_program_thread():
thread = threading.Thread(None, edit_item, None, (), {})
thread.start()
def add_item():
tview.insert("", "end", values=("", "bar"))
def edit_item():
focused = tview.focus()
x = input('Enter a Value you want to change')
tview.item(focused, values=("", str(x)))
tview.pack()
add_item = tk.Button(root, text="Add item", command=add_item)
add_item.pack(expand=True, fill='both')
edit_item_button = tk.Button(root, text="Edit item", command=test_program_thread)
edit_item_button.pack(expand=True, fill='both')
root.mainloop()
To update only specific cell in TTK TreeView no need remove and re-insert as suggested. Instead call ‘set’ method of TreeView
tree.set(my_row_id, column=my_column_id, value=my_new_value)
I have some problem with tkinter/ttk
.
So, i know how to get Treeview.focus, but how to change value of the specific cell in this table? Any suggestions?
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1","SLOT_2")
tview.column("SLOT_1", width=100 )
tview.column("SLOT_2", width=100)
tview.heading("#0",text="Column 0",anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def add_item():
tview.insert("","end",values=("","bar"))
def edit_item():
focused = tview.focus()
print(tview.item(focused))
tview.pack()
add_item = tk.Button(root,text="Add item",command=add_item)
add_item.pack(expand=True,fill='both')
edit_item = tk.Button(root,text="Edit item",command=edit_item)
edit_item.pack(expand=True,fill='both')
root.mainloop()
I’m using Python 3.6
with tkinter/ttk
.
I added a thread so the program doesn’t hang while it waits for the user to enter the edit. You’ll probably want to add a text box or multiple text boxes for the edits to be entered
import tkinter as tk
import tkinter.ttk as ttk
import threading
root = tk.Tk()
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1", "SLOT_2")
tview.column("SLOT_1", width=100)
tview.column("SLOT_2", width=100)
tview.heading("#0", text="Column 0", anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def test_program_thread():
thread = threading.Thread(None, edit_item, None, (), {})
thread.start()
def add_item():
tview.insert("", "end", values=("", "bar"))
def edit_item():
focused = tview.focus()
x = input('Enter a Value you want to change')
tview.insert("", str(focused)[1:], values=("", str(x)))
tview.delete(focused)
tview.pack()
add_item = tk.Button(root, text="Add item", command=add_item)
add_item.pack(expand=True, fill='both')
edit_item_button = tk.Button(root, text="Edit item", command=test_program_thread)
edit_item_button.pack(expand=True, fill='both')
root.mainloop()
Building on Gardener85’s answer.
Using a single .item to update the line instead of adding a new line with .insert and then deleting the old line with .delete will help with a Treeview that contains more than 9 lines and keep the updated line in the same position in the Treeview.
Full code for modified Gardener85’s answer:
tview = ttk.Treeview(root)
tview["columns"] = ("SLOT_1", "SLOT_2")
tview.column("SLOT_1", width=100)
tview.column("SLOT_2", width=100)
tview.heading("#0", text="Column 0", anchor="w")
tview.heading("SLOT_1", text="Column 1")
tview.heading("SLOT_2", text="Column 2")
def test_program_thread():
thread = threading.Thread(None, edit_item, None, (), {})
thread.start()
def add_item():
tview.insert("", "end", values=("", "bar"))
def edit_item():
focused = tview.focus()
x = input('Enter a Value you want to change')
tview.item(focused, values=("", str(x)))
tview.pack()
add_item = tk.Button(root, text="Add item", command=add_item)
add_item.pack(expand=True, fill='both')
edit_item_button = tk.Button(root, text="Edit item", command=test_program_thread)
edit_item_button.pack(expand=True, fill='both')
root.mainloop()
To update only specific cell in TTK TreeView no need remove and re-insert as suggested. Instead call ‘set’ method of TreeView
tree.set(my_row_id, column=my_column_id, value=my_new_value)