http://qs321.pair.com?node_id=952317

bcarroll has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create an interface that allows dragging and dropping buttons, I am currently using Tk:Table to manage the button layout, but am open to other suggestions. The problem i am running into is I can't figure out how to translate the mouse cursor location on the window to the corresponding column and row in the table (findCell subroutine).
The goal is to dynamically create buttons, then drag any button to a new location, shifting the buttons underneath to the left or right.

Any help would be greatly appreciated!

Here is my test script (I removed all of my failed attempts at getting the column and row locations)

use warnings; use strict; use Tk; use Tk::Table; use Data::Dumper; my $buttonsPerRow = 5; $buttonsPerRow -= 1; #button numbering starts from 0... my $row = 0; my $col = 0; 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(); $col>$buttonsPerRow?$row++:undef; $col>$buttonsPerRow?$col=1:$col++; print "Button $buttonNum->$row:$col\n"; $buttons[$buttonNum]{'row'} = $row; $buttons[$buttonNum]{'col'} = $col; $table->put($row,$col,$buttons[$buttonNum]); configureButton($buttonNum); $buttonNum++; }, )->pack(-side=>'top'); ################################ #DragNDrop my $isDragging = 0; #boolean - left mouse button pressed and dragging my $selectedButton = 0; $mw->bind('<Motion>', \&motion); my %tableRows; my %tableCols; ################################ $mw->MainLoop(); sub configureButton{ #configure the new button 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 { #what to do when the mouse is moving my($widget) = @_; my $e = $widget->XEvent;#get Tk Event if ($isDragging){ findCell($e->X,$e->Y); #get mouse cursor X/Y coorinates fro +m event } return unless $isDragging; } sub buttonPress { #what to do when the left mouse button is pressed undef %tableRows; #undefine %tableRows hash in case row height has + changed undef %tableCols; #undefine %tableCols hash in case column width h +as changed for(my $btnNum=0;$btnNum<@buttons;$btnNum++){ #recreate %tableRows + and %tableCols - key=XY_coordinate val=row/col_number $tableRows{$buttons[$btnNum]->rooty} = $buttons[$btnNum]{'row' +}; $tableCols{$buttons[$btnNum]->rootx} = $buttons[$btnNum]{'col' +}; } $isDragging = 1; #Debugging... show root location of each row and column #foreach my $key (sort keys %tableRows){ # print "$key\n"; #} #print "------\n"; #foreach my $key (sort keys %tableCols){ # print "$key\n"; #} } sub buttonRelease { #what to do when the left mouse button is released $isDragging = 0; } sub findCell{ my $tmpX = shift; my $tmpY = shift; print "$tmpX:$tmpY\n"; #determine which row and column the mouse cursor is currently hove +ring over # based on mouse X/Y coordinates. }
  • Comment on Need help finding which column/row the mouse cursor is over in Tk...
  • Download Code

Replies are listed 'Best First'.
Re: Need help finding which column/row the mouse cursor is over in Tk...
by thundergnat (Deacon) on Feb 07, 2012 at 18:36 UTC

    I think you are looking for the "containing" method. Change your findCell{} sub to the below and see if that helps you.

    sub findCell{ my $tmpX = shift; my $tmpY = shift; my ( $row, $col ) = $table->Posn($table->containing($tmpX,$tmpY)); print "$tmpX:$tmpY: Row $row Col $col\n"; }

      That works perfectly! And significantly reduces the amount of code!

      FWIW, Here is the new code.

      use warnings; use strict; use Tk; use Tk::Table; use Data::Dumper; my $buttonsPerRow = 5; $buttonsPerRow -= 1; my $row = 0; 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'); ################################ #DragNDrop my $isDragging = 0; #boolean - left mouse button pressed and dragging my $selectedButton = 0; $mw->bind('<Motion>', \&motion); ################################ $mw->MainLoop(); sub configureButton{ #configure the new button 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 { #what to do when the mouse is moving my($widget) = @_; my $e = $widget->XEvent; if ($isDragging){ findCell($e->X,$e->Y); } return unless $isDragging; } sub buttonPress { $isDragging = 1; } sub buttonRelease { #what to do when the left mouse button is released $isDragging = 0; } sub findCell{ my $tmpX = shift; my $tmpY = shift; my ( $row, $col ) = $table->Posn($table->containing($tmpX,$tmpY)); print "$tmpX:$tmpY: Row $row Col $col\n"; }
Re: Need help finding which column/row the mouse cursor is over in Tk...
by kcott (Archbishop) on Feb 07, 2012 at 18:44 UTC

    As you're open to suggestions regarding an alternative to Tk::Table, how about using Tk::grid. You can use its gridLocation() method to return the row and column - no need to write a custom findCell() subroutine.

    In the widget demo, the code for N-Puzzle (under User Contributed Demonstrations) may be of some use.

    -- Ken