2016-04-01 51 views
0

的名稱,以便基本上我有this樹:的Python 3 Tkinter的樹狀獲得所選項目

而且我希望有按「爲Aggiungi」按鈕,選擇回項目的名稱。 例如,當我選擇番茄,然後按下按鈕我想要「番茄鍾」作爲字符串,如果我選擇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() 
+0

http://stackoverflow.com/questions/34849035/how-to-get-the-value-的可能的複製一個選定的樹視圖項目/ 34855093 –

回答

0

虛擬事件和方法可能是你需要什麼,

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

我得到它的感謝,無視我的文章上的編輯。 – Steve