python 如何改变特定单元格的值?

ix0qys7i  于 2023-01-24  发布在  Python
关注(0)|答案(3)|浏览(177)

我对tkinter/ttk有一些问题。
那么,我知道如何获得Treeview.focus,但是如何改变这个表中特定单元格的值呢?有什么建议吗?

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()

我将Python 3.6tkinter/ttk一起使用。

webghufk

webghufk1#

我添加了一个线程,以便程序在等待用户输入编辑内容时不会挂起。您可能需要添加一个或多个文本框来输入编辑内容

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()
5jvtdoz2

5jvtdoz22#

基于Gardener85's answer构建。
使用单个.item更新行,而不是使用.insert添加新行,然后使用.delete删除旧行,这将有助于处理包含9行以上的树视图,并使更新的行保持在树视图中的相同位置。
修改后的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()
ybzsozfc

ybzsozfc3#

要仅更新TTK TreeView中的特定单元格,无需按照建议删除和重新插入。而是调用TreeView的“set”方法

tree.set(my_row_id, column=my_column_id, value=my_new_value)

相关问题