代码之家  ›  专栏  ›  技术社区  ›  Nicholas Shaffer

从中的选定行获取特定列值树视图使用Python2.7设置

  •  0
  • Nicholas Shaffer  · 技术社区  · 6 年前

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

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

    12 ,u'123 auberndale',1234年, u'0.0',1022018,u'Air Mover',u'Phoenix',1234],“开放”:0,“标签”: ''}

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

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   yoonghm    6 年前

    试试这个

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

    数据是json格式的,您可以使用python字典来访问它。