简体   繁体   English

如何使ttk.Treeview可编辑?

[英]How to make ttk.Treeview editable?

a programming beginner here, I was tasked to do a homework regarding UI in python. 这里是一名编程初学者,我受命做有关python中UI的作业。 The task was a sort of a create, update and delete system(No databases included) but it has to have OOP. 该任务是一种创建,更新和删除系统(不包括数据库)的任务,但它必须具有OOP。 So I decided to use the ttk.Treeview in python. 所以我决定在python中使用ttk.Treeview There was not much trouble in getting create and delete working, but when I tried to to do the update function I was lost and none that I have tried worked so far. 使创建和删除工作并没有太大的麻烦,但是当我尝试执行更新功能时,我迷路了,到目前为止,我没有尝试过。

Regarding the ones I have tried, I tried to apply the answer to How to make ttk.Treeview's rows editable? 关于我尝试过的内容,我尝试将答案应用于如何使ttk.Treeview的行可编辑? . But I could not understand it because I am still just a beginner and every other ones I have seen, they all use databases, which is not allowed in this homework. 但是我无法理解,因为我仍然只是一个初学者,而我所见过的其他所有人,他们都使用数据库,因此本作业不允许这样做。

As for my code it's fairly short: 至于我的代码,它很短:

from tkinter import messagebox, ttk

employees = []

class Employees:

    def __init__(self, n, d, p, r):
        self.n = n
        self.d = d
        self.p = p
        self.r = r


def add():
    n = e1.get()
    d = e2.get()
    p = e3.get()
    r = e4.get()

    employees.append(Employees(n, d, p, r))

    tview.insert('', "end", text=n, values=(d, p, r))
    messagebox.showinfo("Add", "Successfully added")

def delete():
    selected_item = tview.selection()[0]
    tview.delete(selected_item)

def updatetreeview():
    # here where I am lost at I don't know what to do
    selected_item = tview.selection()[0]


master = Tk()

Label(master, text='Name').grid(row=0)
Label(master, text='Department').grid(row=1)
Label(master, text='Position').grid(row=2)
Label(master, text='Rate').grid(row=3)
tview = ttk.Treeview(master, columns=('Name', 'Position', 'Department','Rate'))
tview.grid(row=7, column=0, columnspan=10)

tview.heading('#0', text="Name")
tview.heading('#1', text="Department")
tview.heading('#2', text="Position")
tview.heading('#3', text="Rate")


e1 = Entry(master, width="30")
e2 = Entry(master, width="30")
e3 = Entry(master, width="30")
e4 = Entry(master, width="30")

e1.grid(row=0, column=1, pady=10)
e2.grid(row=1, column=1, pady=10)
e3.grid(row=2, column=1, pady=10)
e4.grid(row=3, column=1, pady=10)

b1 = Button(master, text="Add", width="25", command=add)
b1.grid(row=4, column=1, pady=10)
b2 = Button(master, text="Update", width="25")
b2.grid(row=5, column=1, pady=10)
b2 = Button(master, text="Delete", width="25", command=delete)
b2.grid(row=6, column=1, pady=10)
mainloop()

As for my expected results, I expect for the selected row in the table to be updated upon a button press. 至于我的预期结果,我希望表中的选定行在按下按钮时会更新。

I believe the word you are looking for should be "updating" a Treeview item instead of editable. 我相信您要查找的单词应该是“更新” Treeview项,而不是可编辑的。 From the docs : 文档

item(item, option=None, **kw) 项目(项目,选项=无,**千瓦)

Query or modify the options for the specified item. 查询或修改指定项目的选项。

If no options are given, a dict with options/values for the item is returned. 如果没有给出选项,则返回包含该选项的选项/值的字典。 If option is specified then the value for that option is returned. 如果指定了option,则返回该选项的值。 Otherwise, sets the options to the corresponding values as given by kw. 否则,将选项设置为kw给定的相应值。

So for your case, you have retrieved the iid already by tview.selection()[0] . 因此,对于您的情况,您已经通过tview.selection()[0]检索了iid。 Use the item method to modify the record: 使用item方法修改记录:

def updatetreeview():
    selected_item = tview.selection()[0]
    tview.item(selected_item,text=e1.get(), values=(e2.get(),e3.get(),e4.get()))

...

b2 = Button(master, text="Update", width="25",command=updatetreeview)

...

Your function to update the content in the row is incomplete, you are retrieving the content using the treeview.selection() method after that you have to use treeview item to update the content in the treeview . 你的功能更新的内容row是不完整的,您检索使用内容treeview.selection()之后,你必须使用方法treeview item来更新内容treeview

Have added exception handling to the update function to prompt you to select the error you want to update if not it will output an error. 在更新功能中添加了exception处理,以提示您选择要更新的错误,否则将输出错误。

Also have to clear the entry after you add or update the treeview . 添加或更新treeview后,还必须清除条目。

from tkinter import messagebox, ttk
from tkinter import *

employees = []

class Employees:

    def __init__(self, n, d, p, r):
        self.n = n
        self.d = d
        self.p = p
        self.r = r


def add():
    n = e1.get()
    d = e2.get()
    p = e3.get()
    r = e4.get()


    employees.append(Employees(n, d, p, r))

    tview.insert('', "end", text=n, values=(d, p, r))
    messagebox.showinfo("Add", "Successfully added")

    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)


def delete():
    try:
        selected_item = tview.selection()[0]
        tview.delete(selected_item)
    except IndexError:
        pass


def updatetreeview():
    try:
        selected_item = tview.selection()[0]
        tview.item(selected_item, text=e1.get(), values=(e2.get(), e3.get(), e4.get()))
        print("updated")

        e1.delete(0, END)
        e2.delete(0, END)
        e3.delete(0, END)
        e4.delete(0, END)

    except IndexError:
        messagebox.showerror("Error","Select the row you want to update")



master = Tk()

Label(master, text='Name').grid(row=0)
Label(master, text='Department').grid(row=1)
Label(master, text='Position').grid(row=2)
Label(master, text='Rate').grid(row=3)
tview = ttk.Treeview(master, columns=('Name', 'Position', 'Department','Rate'))
tview.grid(row=7, column=0, columnspan=10)

tview.heading('#0', text="Name")
tview.heading('#1', text="Department")
tview.heading('#2', text="Position")
tview.heading('#3', text="Rate")


e1 = Entry(master, width="30")
e2 = Entry(master, width="30")
e3 = Entry(master, width="30")
e4 = Entry(master, width="30")

e1.grid(row=0, column=1, pady=10)
e2.grid(row=1, column=1, pady=10)
e3.grid(row=2, column=1, pady=10)
e4.grid(row=3, column=1, pady=10)

b1 = Button(master, text="Add", width="25", command=add)
b1.grid(row=4, column=1, pady=10)
b2 = Button(master, text="Update", width="25", command=updatetreeview)
b2.grid(row=5, column=1, pady=10)
b2 = Button(master, text="Delete", width="25", command=delete)
b2.grid(row=6, column=1, pady=10)

mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM