Python 3 tkinter treeview获取所选项目的NAME

时间:2016-04-01 16:29:25

标签: python-3.x tkinter treeview selected

所以基本上我有image树:

我想通过按“aggiungi”按钮选择所选项目的名称。 例如,当我选择Pomodori然后按下我想要“Pomodori”的按钮作为字符串,如果我选择Frutta我想要“Frutta”等等...... 我尝试使用get.children然后使用tree.set()但我无法使用它。 我该怎么做?

from tkinter import *
from tkinter import ttk

_root = Tk()
_root.resizable(width=FALSE, height=FALSE)

_F_shopItems = Frame(_root)
_F_shopItems.pack(side="left")

_F_treeview = Frame(_root)
_F_treeview.pack(side="left")

_F_bottom = Frame(_root)
_F_bottom.pack(side="bottom")

# ------------------------
# Treeview
_T_listaProd = ttk.Treeview(_F_treeview, height=10)
_T_listaProd.column("#0", width=165)
_T_listaProd.heading("#0", text="Prodotti")
_T_listaProd.config(columns="Prezzi")
_T_listaProd.column("Prezzi", width=75, anchor="center")
_T_listaProd.heading("Prezzi", text="Prezzi (€/Kg)")
_T_listaProd.grid(row=0, column=0)

# Categorie di prodotti
_T_listaProd.insert('', '0', 'Verdura', text='Verdura')
_T_listaProd.insert('', '1', 'Frutta', text='Frutta')
_T_listaProd.insert('', '2', 'Carne', text='Carne')
_T_listaProd.insert('', '3', 'Dolci', text='Dolci')

# Verdura
_T_listaProd.insert('Verdura', 'end', 'V1', text='Insalata Gentile')
_T_listaProd.set("V1", "Prezzi", "0,98")
_T_listaProd.insert('Verdura', 'end', 'V2', text='Cipolla Bianca')
_T_listaProd.set("V2", "Prezzi", "0,98")
_T_listaProd.insert('Verdura', 'end', 'V3', text='Radicchio')
_T_listaProd.set("V3", "Prezzi", "0,98")
_T_listaProd.insert('Verdura', 'end', 'V4', text='Pomodori')
_T_listaProd.set("V4", "Prezzi", "0,98")
_T_listaProd.insert('Verdura', 'end', 'V5', text='Peperoni')
_T_listaProd.set("V5", "Prezzi", "0,98")

# Frutta
_T_listaProd.insert('Frutta', 'end', 'F1', text='Mele rosse')
_T_listaProd.set("F1", "Prezzi", "0,98")
_T_listaProd.insert('Frutta', 'end', 'F2', text='Ciliege')
_T_listaProd.set("F2", "Prezzi", "0,98")

# Dolci
_T_listaProd.insert('Dolci', 'end', 'D1', text='Crostata (Marmellata)')
_T_listaProd.set("D1", "Prezzi", "0,98")
_T_listaProd.insert('Dolci', 'end', 'D2', text='Nutella')
_T_listaProd.set("D2", "Prezzi", "0,98")

# Carne
_T_listaProd.insert('Carne', 'end', 'C1', text='Braciola di Maiale')
_T_listaProd.set("C1", "Prezzi", "0,98")
_T_listaProd.insert('Carne', 'end', 'C2', text='Pancetta')
_T_listaProd.set("C2", "Prezzi", "0,98")
_T_listaProd.insert('Carne', 'end', 'C3', text='Salame')
_T_listaProd.set("C3", "Prezzi", "0,98")

# Scrollbar
_S_listaProd = Scrollbar(_F_treeview, orient=VERTICAL, command=_T_listaProd.yview)
_S_listaProd.grid(row=0, column=1, sticky="nswe")
_T_listaProd.config(yscrollcommand=_S_listaProd.set)
# ------------------------
_T_listaProd.bind('<<TreeviewSelect>>', #To do#)



_B_addItem = Button(_F_bottom, text="aggiungi", command = #To do#)
_B_addItem.pack(side="bottom") 

_main.mainloop()

1 个答案:

答案 0 :(得分:0)

虚拟事件<<TreeviewSelect>>和方法ttk.Treeview.selection可能就是您所需要的,

import tkinter as tk
from tkinter import ttk

class Frame(ttk.Frame):
    def __init__(self, *args, **kw):
        ttk.Frame.__init__(self, *args, **kw)
        self.tree = ttk.Treeview(self)
        self.tree.pack()

        for i in range(10):
            self.tree.insert("", tk.END, text="item %s" % i)

        self.tree.bind('<<TreeviewSelect>>', self.on_select)

        self.button = ttk.Button(self, text="test", command=self.print_selected)

        self.button.pack()
        self.selected = []

    def on_select(self, event):
        self.selected = event.widget.selection()

    def print_selected(self):
        for idx in self.selected:
            print(self.tree.item(idx)['text'])

if __name__ == "__main__":
    root = tk.Tk()
    frame = Frame(root)
    frame.pack()
    root.mainloop()