in reply to Re: Perl Tk , MainLoop, destroy function problem in thread Perl Tk , MainLoop, destroy function problem
Hi zentara,
I need a help regarding configuring this hlist.
The _insertData() is working for the first time, but next time when it gets invoked thru repeat(), I am getting the following error :
XS_Tk__Callback_Call error:element "0" already exists at /opt/perl_5.8.8/lib/Tk.pm line 250.
I ma stuck here, please help. Not able to figure ou from the tar files you sent me.
Re^3: Perl Tk , MainLoop, destroy function problem
by zentara (Archbishop) on Feb 21, 2013 at 12:18 UTC
|
#!/usr/bin/perl
use strict;
use Tk;
use Tk::HList;
my $mw = MainWindow->new();
#create some sample data
my %data;
foreach (0..100) {
$data{$_}{'name'} = 'name'.$_;
$data{$_}{'id'} = rand(time);
}
#get random list of keys
my @keys = keys %data;
#################
my $h = $mw->Scrolled( 'HList',
-header => 1,
-columns => 2,
-width => 30,
-height => 60,
-takefocus => 1,
-background => 'steelblue',
-foreground =>'snow',
-selectmode => 'single',
-selectforeground => 'pink',
-selectbackground => 'black',
# -browsecmd => \&browseThis,
)->pack(-side => "left", -anchor => "n");
my $nameh = $h->header('create', 0, -text => ' Name ',
-borderwidth => 3,
-headerbackground => 'steelblue',
-relief => 'raised');
my $idh = $h->header('create', 1, -text => ' ID ',
-borderwidth => 3,
-headerbackground => 'lightsteelblue',
-relief => 'raised');
foreach (@keys) {
my $e = $h->addchild(""); #will add at end
$h->itemCreate ($e, 0,
-itemtype => 'text',
-text => $data{$_}{'name'},
);
$h->itemCreate($e, 1,
-itemtype => 'text',
-text => $data{$_}{'id'},
);
}
my $button = $mw->Button(-text => 'exit',
-command => sub{exit})->pack;
my $sortid = $mw->Button(-text => 'Sort by Id',
-command => [\&sort_me,1] )->pack;
MainLoop;
#########################################################
sub sort_me{
my $col = shift;
my @entries = $h->info('children');
my @to_be_sorted =();
foreach my $entry(@entries){
push @to_be_sorted,
[ $h->itemCget($entry,0,'text'),
$h->itemCget($entry,1,'text')
];
}
my @sorted = sort{ $a->[$col] cmp $b->[$col] } @to_be_sorted;
my $entry = 0;
foreach my $aref (@sorted){
# print $aref->[0],' ',$aref->[1],"\n";
$h->itemConfigure( $entry, 0, 'text' => $aref->[0] );
$h->itemConfigure( $entry, 1, 'text' => $aref->[1] );
$entry++;
}
$mw->update;
}
| [reply] [d/l] |
|
Hi
Thank you for your reply !
But still one more thing is not working, though I am following the cpan doc for HList. I have tried using both itemExists($entrypath,$col) and info(exists, $entrypath), but of no use.
The scenario is like :
The gui always has two columns , no issue on that.
But first time the gui has one row, so entrypath 0 is added and I used itemCreate() to populate values.
Next time due to updation of the input file the gui should have some 3 rows with entirely new data. This time for entrypath 0 , the if(itemExists) returns true and I am using itemConfigure inside the if block. All fine so far.
But next iteration for entrypath 1, itemExists should fail and I should be able to go to the else block to add() and itemCreate(), but I am not able to.
I am getting following error :
XS_Tk__Callback_Call error:Entry "1" not found at /user/perl_5.8.8/lib/Tk.pm line 250.
Tk::Error: Entry "1" not found at /opt/perl_5.8.8/lib/Tk.pm line 250.
Tk::Error:: Too many arguments.
Tk callback for .frame1.frame.hlist
Tk::After::repeat at /opt/perl_5.8.8/lib/Tk/After.pm line 79
[repeat,[{},after#12,5000,repeat,\&main::clear_data]]
[repeat,[{},after#12,5000,repeat,&main::clear_data]]: No match.
Here is the code snipppet which is not working
if($hl->itemExists($path,0)){
#if($hl->info(exists, $path)){ # tried both info and itemExist alterna
+tely, but :(
print "itemexists ...\n";
$hl->itemConfigure($path,0,-text=> $hash->{$user}->{$tool}->{tool}
+);
$hl->itemConfigure($path,1,-text=> $hash->{$user}->{$tool}->{issue
+d})
;
$hl->itemConfigure($path,2,-text=> $hash->{$user}->{$tool}->{inuse
+});
$hl->itemConfigure($path,3,-text=> $hash->{$user}->{$tool}->{in
+use},
-style => $color);
}
else
{
print "Inside else ....\n";
$hl->add($path);
$hl->itemCreate($path,0,-text=> $hash->{$user}->{$tool}->{tool});
$hl->itemCreate($path,1,-text=> $hash->{$user}->{$tool}->{issued})
;
$hl->itemCreate($path,2,-text=> $hash->{$user}->{$tool}->{inuse});
$hl->itemCreate($path,3,-text=> $hash->{$user}->{$tool}->{inuse},
-style => $color);
}
| [reply] [d/l] |
|
You didn't provide a complete code snippet which demonstrates your problem, but from your code it looks like you have the right concept. However, from the error message you provided, it says you have "too many arguments", and just from
my intuition, I think the problem is that you say you have just 2 columns, yet you are trying to itemCreate 4 in your add($path).
Related to that, and I'm not sure about it, but if you know that you have 4 columns
in your subsequent data, but only 1 initially, you may want to create that first row with 4 columns with the last 3 empty, thereby making available for itemConfigure. Otherwise, you will need to figure out how to change that entry from a 1 column to a 4 column path. I hope that makes sense.
Another possible problem is that you may be running into conflicts with your 2 timers. Where 1 timer is clearing out data while another is trying to write, but we don't see what your code is actually doing. Can you make a small running example with some fake data, that demonstrates your problem?
| [reply] |
|
|
|
|