当前位置: 代码迷 >> python >> 使用Python 2.7从tree.view设置的选定行中获取特定的列值
  详细解决方案

使用Python 2.7从tree.view设置的选定行中获取特定的列值

热度:108   发布时间:2023-06-19 09:29:12.0

我有一个正在编写的清单程序,我需要从用户选择的重点行中获取特定列的数据。

def ShowChangeForm():
    curitem = tree.focus()
    print tree.item(curitem)

下面是上面代码的当前输出

{'text':'','image':'','values':[ 12 ,u'123 auberndale',1234,u'0.0',1022018,u'Air Mover',u'Phoenix',1234] ,'open':0,'tags':''}

我的树代码如下

def ViewForm():
    global tree
    TopViewForm = Frame(Home, width=600, bd=1, relief=SOLID)
    TopViewForm.pack(side=TOP, fill=X)
    LeftViewForm = Frame(Home, width=600)
    LeftViewForm.pack(side=LEFT, fill=Y)
    MidViewForm = Frame(Home, width=1000)
    MidViewForm.pack(fill=X)
    lbl_text = Label(TopViewForm, text="View Products", font=('arial', 18), width=600)
    lbl_text.pack(fill=X)
    lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
    lbl_txtsearch.pack(side=TOP, anchor=W)
    search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15), width=10)
    search.pack(side=TOP,  padx=10, fill=X)
    btn_search = Button(LeftViewForm, text="Search", command=Search)
    btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
    btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
    btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
    btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
    btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
    scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
    scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
    tree = ttk.Treeview(MidViewForm, columns=("Product ID", "Location", "Equipment #", "Price Per Day", "DatePlaced", "Type", "Make", "Model", "Total"), selectmode="extended", height=100, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
    scrollbary.config(command=tree.yview)
    scrollbary.pack(side=RIGHT, fill=Y)
    scrollbarx.config(command=tree.xview)
    scrollbarx.pack(side=BOTTOM, fill=X)
    tree.heading('Product ID', text="Product ID",anchor=W)
    tree.heading('Location', text="Location",anchor=W)
    tree.heading('DatePlaced', text="DatePlaced",anchor=W)
    tree.heading('Equipment #', text="Equipment #",anchor=W)
    tree.heading('Type', text="Type",anchor=W)
    tree.heading('Price Per Day', text="Price Per Day",anchor=W)
    tree.heading('Make', text="Make",anchor=W)
    tree.heading('Model', text="Model",anchor=W)
    tree.heading('Total', text="Total",anchor=W)
    tree.column('#0', stretch=NO, minwidth=0, width=0)
    tree.column('#1', stretch=NO, minwidth=0, width=0)
    tree.column('#2', stretch=NO, minwidth=0, width=100)
    tree.column('#3', stretch=NO, minwidth=0, width=100)
    tree.column('#4', stretch=NO, minwidth=0, width=100)
    tree.column('#5', stretch=NO, minwidth=0, width=100)
    tree.column('#6', stretch=NO, minwidth=0, width=100)
    tree.column('#7', stretch=NO, minwidth=0, width=100)
    tree.column('#8', stretch=NO, minwidth=0, width=100)
    tree.column('#9', stretch=NO, minwidth=0, width=100)
    tree.pack(fill=X)
    DisplayData()

需要的是所选列的第一行(在值列表中,值“ 12 ”上方的粗体将分配给可以传递给另一个函数的变量。如何获取所选行,但仅获取第一列值并将其分配给变量?

尝试这个

print tree.item(curitem)['values'][0]

数据为json格式,您可以使用python字典进行访问。

  相关解决方案