| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 
 |  
import Tix as tk
from tkMessageBox import showerror,askyesno,showinfo
 
def Hook(event):
    """ called by double clik on item"""
    global tree #I haven't found yet an other way...
    showinfo("DEBUG","%s"%(GetLabelByName(tree,event)))
 
def Refresh(tree):
    """ adjust mode when status parent - child changes"""
    for name in tree.names_dicco.values():
        _oldmode = tree.getmode(name)
        if len(tree.hlist.info_children(name)):
            if _oldmode != "none":
                tree.setmode(name,_oldmode)
            else:
                tree.setmode(name,"close")
        else:
            tree.setmode(name,"none")
 
def AddChild(tree,label,parent = None):
    """ add a branch on the tree"""
    if label in tree.names_dicco.keys():
        return 0
    if parent == None:
        #no parent, root level
        _name = tree.hlist.add(label,text = label)
    else:
        #there is a parent, let's do a child
        _name = tree.hlist.add_child(parent = tree.names_dicco[parent],text = label)
    tree.names_dicco[label] = _name
    Refresh(tree)
    return 1
 
def GetLabelByName(tree,label):
    for _key in tree.names_dicco.keys():
        if tree.names_dicco[_key] == label:
            return _key
    return None
 
if __name__ == "__main__":
 
    root = tk.Tk()
    root.geometry("+50+50")
    tree   = tk.Tree(root,width = 640,height = 480,command = Hook)
    tree.hlist.configure(selectmode = "browse")
    tree.names_dicco = {}
    tree.grid()
    # tree creation
    for idx in range(1,11):
        label = "DynamicPage%02u"%(idx)
        AddChild(tree,label)
    AddChild(tree,"MainMenu")
    AddChild(tree,"Parameters","MainMenu")
    AddChild(tree,"Display","Parameters")
    AddChild(tree,"Line 1","Display")
    AddChild(tree,"L1ProcVar","Line 1")
    AddChild(tree,"L1FavUnit","Line 1")
    AddChild(tree,"L1Filter","Line 1")
    AddChild(tree,"Line 2","Display")
    AddChild(tree,"L2ProcVar","Line 2")
    AddChild(tree,"L2FavUnit","Line 2")
    AddChild(tree,"L2Filter","Line 2")
    AddChild(tree,"Calibration","MainMenu")
    AddChild(tree,"CalPoint1","Calibration")
    AddChild(tree,"CalPoint2","Calibration")
    AddChild(tree,"CalSave","Calibration")
    AddChild(tree,"Test","MainMenu")
 
    tree.mainloop() | 
Partager