My latest Tk experiment was to have a widget in a window that, when clicked and dragged, would move the window it was in (or basically, to enable me to do overrideredirect to remove the window chrome, and make my own title bar widget that could be dragged to move the window around).
Here's the code I have so far:
It creates a 400x400 orange window a little ways away from the top left corner of the screen. When I click and drag, it only partly works.
By this I mean, the window does slowly move in the direction I'm dragging it, but while it's moving, it also keeps flickering in another position up and to the left of its current one. So as I drag it, it flickers back and forth between two positions slowly moving in the direction I'm dragging. When I let go of the mouse, it will pick one of these positions to stay in.
Maybe I'm just not thinking mathematically enough, but can anyone point out what might be a problem in my code?
Thanks in advance.
Here's the code I have so far:
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Tk;
my $main = MainWindow->new;
$main->overrideredirect(1);
$main->geometry ('400x400+10+20');
my $dragger = $main->Frame (-background => 'orange')->pack (-fill => 'both', -expand => 1);
my $winX = 10;
my $winY = 20;
my $dragFromX = 0;
my $dragFromY = 0;
my $isDragging = 0;
$dragger->bind ('<ButtonPress-1>', sub {
$isDragging = 1;
# dragFrom vars should be the offset from 0,0 to the current position.
$dragFromX = $Tk::event->x;
$dragFromY = $Tk::event->y;
print "Drag from: $dragFromX,$dragFromY\n";
});
$dragger->bind ('<ButtonRelease-1>', sub {
$isDragging = 0;
});
$dragger->bind ('<Motion>', sub {
return unless $isDragging;
# Get the new position.
my $curX = $Tk::event->x;
my $curY = $Tk::event->y;
$winX = $curX;
$winY = $curY;
print "MoveTo: $winX,$winY\n";
$main->MoveToplevelWindow ($winX,$winY);
});
MainLoop;
It creates a 400x400 orange window a little ways away from the top left corner of the screen. When I click and drag, it only partly works.
By this I mean, the window does slowly move in the direction I'm dragging it, but while it's moving, it also keeps flickering in another position up and to the left of its current one. So as I drag it, it flickers back and forth between two positions slowly moving in the direction I'm dragging. When I let go of the mouse, it will pick one of these positions to stay in.
Maybe I'm just not thinking mathematically enough, but can anyone point out what might be a problem in my code?
Thanks in advance.