#!/usr/local/bin/perl use strict; use warnings; use Tk; use Tk::MListbox; use Tk::DialogBox; use Tk::LabEntry; use Data::Dumper; #main window my $mw = MainWindow->new(-title=>'Main Window'); my $frame_top = $mw->Frame-> pack(-side=>'top',-fill => 'both', -anchor,'nw'); my $frame_bottom = $mw->Frame-> pack(-side=>'top',-fill => 'both', -anchor,'nw'); my $edit_button = $mw->Button(-text => "Edit Tasks",-state => 'normal',-width => 30,-activebackground=>'gray', -command => sub{edit_tasks()})->pack (-in => $frame_top,-side=>'top',-padx => 3,-pady => 5); my $mw_close_button = $mw->Button(-text => 'Close',-background=>'gray',-width=>60,-command => sub{$mw->destroy})->pack(-in=>$frame_bottom,-side => 'top',-expand => '0',-fill => 'x'); MainLoop(); sub edit_tasks { # load your data my $tasks=load_data(); # create toplevel window my $top=$mw->Toplevel(-title=>"Toplevel Window"); # create toplevel frames my $frame_top = $top->Frame-> pack(-side=>'top',-fill => 'both', -anchor,'nw'); my $frame_bottom = $top->Frame-> pack(-side=>'top',-fill => 'both', -anchor,'nw'); my $tasklist = $top->Scrolled('MListbox', -scrollbars => 'ose',-separatorcolor => 'darkgrey',-height=>15,-width=>300)->pack(-in=>$frame_top,-expand=>1,-fill=>'both'); $tasklist->columnInsert('end',-text=>'Task',-width=>15); $tasklist->columnInsert('end',-text=>'Date',-width=>15); $tasklist->columnInsert('end',-text=>'Time',-width=>15); #buttons my $add_button = $top->Button(-text => "Add Task",-state => 'normal',-width => 30,-activebackground=>'gray', -command => sub{add_task($top,\$tasks); fill_tasklist($tasklist,\$tasks);})->pack (-in => $frame_top,-side=>'top',-padx => 3,-pady => 5); my $refresh_button = $top->Button(-text => "Refresh",-state => 'normal',-width => 30,-activebackground=>'gray', -command => sub{fill_tasklist($tasklist,\$tasks);})->pack (-in => $frame_top,-side=>'top',-padx => 3,-pady => 5); my $mw_close_button = $top->Button(-text => 'Close',-background=>'gray',-width=>60,-command => sub{$top->destroy})->pack(-in=>$frame_bottom,-side => 'top',-expand => '0',-fill => 'x'); fill_tasklist($tasklist,\$tasks); } sub add_task { my $parent=shift; my $tasksref=shift; my $tasks=$$tasksref; my $ewidth=40; my $thistaskname; my $thisdate; my $thisstart; my $thisstop; my $add_dialog = $parent->DialogBox( -title => "Add task",-buttons => [ "Save", "Cancel" ] ); my $task_entry=$add_dialog->LabEntry(-label => "Task ", -bg=>'white', -labelFont => 'monospaced8', -labelPack => [-side => "left", -anchor => "w"], -width => 30, -textvariable => \$thistaskname)->pack(-side => "top", -anchor => "nw", -fill=>'x'); my $date_entry1=$add_dialog->LabEntry(-label => "Date ", -bg=>'white', -labelFont => 'monospaced8', -labelPack => [-side => "left", -anchor => "w"], -width => $ewidth, -textvariable => \$thisdate)->pack(-side => "top", -anchor => "nw", -fill=>'x'); my $time_start_entry=$add_dialog->LabEntry(-label => "Time Start ", -bg=>'white', -labelFont => 'monospaced8', -labelPack => [-side => "left", -anchor => "w"], -width => $ewidth, -textvariable => \$thisstart)->pack(-side => "top", -anchor => "nw", -fill=>'x'); my $time_stop_entry=$add_dialog->LabEntry(-label => "Time Stop ", -bg=>'white', -labelFont => 'monospaced8', -labelPack => [-side => "left", -anchor => "w"], -width => $ewidth, -textvariable => \$thisstop)->pack(-side => "top", -anchor => "nw", -fill=>'x'); $task_entry->focus; #set focus depending on type of command (add,edit) and erros (alias,user or password invalid) my $clicked_button = $add_dialog->Show; if ($clicked_button eq 'Save') { my $thistime="$thisstart\-$thisstop"; push(@{$tasks->{$thistaskname}->{$thisdate}},$thistime); } } sub fill_tasklist { my $tasklistwidget=shift; my $tasksref=shift; my $tasks=$$tasksref; $tasklistwidget->delete(0,'end'); foreach my $thistaskname (sort keys %{$tasks}) { foreach my $thisdate (sort keys %{$tasks->{$thistaskname}}) { foreach my $thistime (@{$tasks->{$thistaskname}->{$thisdate}}) { my @row; push(@row,$thistaskname); push(@row,$thisdate); push(@row,$thistime); $tasklistwidget->insert('end', [@row]); $tasklistwidget->see('end'); $tasklistwidget->update; } } } print Dumper($tasks); } sub load_data { my $tasks; my $pos= tell(DATA); while () { chomp $_; my @line=split(/,/,$_); my $thistaskname=$line[0]; my $thisdate=$line[1]; my $thisstart=$line[2]; my $thisstop=$line[3]; my $thistime="$thisstart\-$thisstop"; push(@{$tasks->{$thistaskname}->{$thisdate}},$thistime); } seek DATA, $pos, 0; return $tasks; } __DATA__ Task1,20110329,08:00,12:00 Task2,20110329,13:00,18:00 Task3,20110330,09:00,17:00