bcarroll has asked for the wisdom of the Perl Monks concerning the following question:
I created a little test (slideButton() subroutine) that puts the dragged button in the cell the mouse is hovering over.
Many thanks to thundergnat for help with the findCell() sub...
...Updated code...
I fixed a couple of things in slideButton(). Now when a button is dragged the button it is over moves to the row/col the dragged button was previsouly in, but I can't seem to get the actual button widget to be displayed.
Note this line in slideButton():
I can display the button's text, but if I remove "->cget(-text)" nothing is displayed... Any ideas?$table->put($displacedButton->{'row'},$displacedButton->'col'},$displa +cedButton->cget(-text));
use warnings; use strict; use Tk; use Tk::Table; use Data::Dumper; my $buttonsPerRow = 5; $buttonsPerRow -= 1; my $row = 1; my $col = 1; my $buttonNum = 0; my @buttons; my $mw = tkinit(); my $menuFrame = $mw->Frame()->pack(-expand=>1,-fill=>'both'); my $buttonFrame = $mw->Frame()->pack(-expand=>1,-fill=>'both'); my $table = $buttonFrame->Table( -columns => 5, -scrollbars => 'se', -fixedrows => 1, )->pack(-expand=>1,-fill=>'both'); my $addButton = $menuFrame->Button( -text => 'Add Button', -command => sub{ $buttons[$buttonNum] = $table->Button(); print "Button $buttonNum->$row:$col\n"; $buttons[$buttonNum]{'row'} = $row; $buttons[$buttonNum]{'col'} = $col; $table->put($row,$col,$buttons[$buttonNum]); configureButton($buttonNum); $col>$buttonsPerRow?$row++:undef; $col>$buttonsPerRow?$col=1:$col++; $buttonNum++; }, )->pack(-side=>'top'); ################################ my $isDragging = 0; #boolean - left mouse button pressed and dragging my $selectedButton = 0; $mw->bind('<Motion>', \&motion); ################################ $mw->MainLoop(); sub configureButton{ my $btnNum = shift; $buttons[$btnNum]->bind('<Enter>', sub{ $selectedButton = $btnNum; } ); $buttons[$btnNum]->configure( -text => "Button $btnNum", -command => sub{ print "Button $btnNum pressed\n"; } ); $buttons[$btnNum]->bind('<ButtonPress-1>', \&buttonPress); $buttons[$btnNum]->bind('<ButtonRelease-1>', \&buttonRelease); } sub motion { my($widget) = @_; my $e = $widget->XEvent; if ($isDragging){ slideButton(findCell($e->X,$e->Y)); } return unless $isDragging; } sub buttonPress { $isDragging = 1; } sub buttonRelease { $isDragging = 0; } sub findCell{ my $tmpX = shift; my $tmpY = shift; my ( $row, $col ) = $table->Posn($table->containing($tmpX,$tmpY)); return ($row,$col,$tmpX,$tmpY); } sub slideButton{ my ($mRow,$mCol,$tmpX,$tmpY) = @_; return unless $mRow; print $buttons[$selectedButton]{'row'} . ":" . $buttons[$selectedB +utton]{'col'} . "\n"; my $displacedButton = $table->containing($tmpX,$tmpY); $displacedButton->{'row'} = $buttons[$selectedButton]{'row'}; $displacedButton->{'col'} = $buttons[$selectedButton]{'col'}; $table->put($displacedButton->{'row'},$displacedButton->{'col'},$d +isplacedButton->cget(-text)); $table->put($mRow,$mCol,$buttons[$selectedButton]); $buttons[$selectedButton]{'row'} = $mRow; $buttons[$selectedButton]{'col'} = $mCol; print "Row:$mRow : Col: $mCol\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to make a Tk::Button move/slide within a Tk::Table
by kcott (Archbishop) on Feb 08, 2012 at 20:52 UTC | |
by bcarroll (Pilgrim) on Feb 08, 2012 at 20:53 UTC | |
by kcott (Archbishop) on Feb 08, 2012 at 22:04 UTC | |
by bcarroll (Pilgrim) on Feb 09, 2012 at 12:14 UTC | |
|
Re: How to make a Tk::Button move/slide within a Tk::Table
by Anonymous Monk on Feb 08, 2012 at 22:30 UTC | |
|
Re: How to make a Tk::Button move/slide within a Tk::Table
by thundergnat (Deacon) on Feb 09, 2012 at 17:01 UTC | |
by bcarroll (Pilgrim) on Feb 09, 2012 at 19:22 UTC |