#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Tk; use Tk::Tree; use Tk::Frame; use Tk::Label; use Tk::Entry; use TK::Button; use YAML qw/Dump DumpFile LoadFile/; my $mw = MainWindow->new(); my $tree = $mw->ScrlTree( -command => sub { warn Dumper \@_ }, -indicatorcmd => sub { }, -selectbackground => 'lightgrey', -width => 25, -height => 12, -scrollbars => 'se' )->pack( -expand => '1', -fill => 'both' ); # add EnerTree->add_hook for TK callback # use lookup-table to resolve EnerTree-entries by distinguished name my $item = $tree->add( 'HAK', -text => 'HAK63' ); my $current_root = ($tree->info('children'))[0];; my $frame1 = $mw->Frame()->pack(); my $label1 = $frame1->Label( -text => '' )->pack(); my $entry1 = $frame1->Entry()->pack(-side => 'left'); my $btn1 = $frame1->Button( -command => sub { warn $entry1->get(); $tree->addchild( $current_root, -text => $entry1->get()); }, -text => 'add entry', )->pack(-side => 'left'); my $btn2 = $frame1->Button( -command => sub { store_tree($tree) }, -text => 'save', )->pack(-side => 'left'); my $btn3 = $frame1->Button( -command => sub { load_tree() }, -text => 'load', )->pack(-side => 'left'); $tree->configure( -browsecmd => sub { my $item = shift; $label1->configure( -text => $item ); $current_root = $item; } ); sub store_tree { my $tree = shift; my $file = shift || 'tree.conf'; my $path = ($tree->info('children'))[0]; my %all; $all{$path} = $tree->entrycget($path, '-text'); while (my $p = $tree->info('next', $path)) { $all{$p} = $tree->entrycget($p, '-text'); $path = $p; } DumpFile($file, \%all) } sub load_tree { my $file = shift || 'tree.conf'; my $conf = LoadFile($file); $tree->delete('all'); $tree->add($_, -text => $conf->{$_}) for sort keys %$conf; } MainLoop;