Thread TopLevel-Fenster schließen, wenn Klick außerhalb (14 answers)
Opened by Gast at 2008-01-10 15:32

Gast Gast
 2008-01-11 10:01
#104607 #104607
Hallo,

danke schon mal für die Antworten. Mein Code sieht zunächst so aus
Code (perl): (dl )
1
2
3
4
5
6
7
### Test.pl
use KMenueTest;

use Tk;
my $w = KMenueTest->new;

MainLoop;


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package KMenueTest;

use strict;
use Tk;
use Tk::Toplevel;

sub new
{
    my($class) = @_;
    my $this = bless({
        MW => MainWindow->new
       }, $class);
    
    my $lab = $this->{MW}->Label(-text => 'Klick hier')->pack;
    
    $this->{MW}->bind($lab, '<1>' => [ 
        \&Click_Event, $this, Ev('X'), Ev('Y') ]
    );
    
    return $this;
}

sub Click_Event
{
    my($mw, $this, $x, $y) = @_;
    
    $this->Close_KMenue if $this->{KMenueOpen};
    
    my $kmenue = $mw->Toplevel(qw/-bd 1 -relief raised/);
    
    $kmenue->overrideredirect(1);
    $kmenue->geometry(sprintf("+%d+%d", $x, $y));
    
    my %opt = qw/-activebackground darkblue 
        -activeforeground white 
        -width 20 -height 1 -bd 0 -anchor w/;
    
    $kmenue->Button(%opt, -text => "Test1"
        , -command => [ \&Test, $this, 1 ])->pack;
    
    $kmenue->Button(%opt, -text => "Test2"
        , -command => [ \&Test, $this, 2 ])->pack;
    
    $this->{KMenue} = $kmenue;
    $this->{KMenueOpen} = 1;
}

sub Test
{
    my($this, $param) = @_;
    print "Test $param\n";
    $this->Close_KMenue;
}

sub Close_KMenue
{
    my($this) = @_;
    $this->{KMenue}->destroy;
    $this->{KMenue} = undef;
    $this->{KMenueOpen} = 0;
}

1;


Wenn ich auf den Label klicke, wird das Menü angezeigt. Wie kann ich das Menü schließen, wenn auch außerhalb von MainWindow geklickt wird. Also Desktop bspw.?

Vielen Dank!

View full thread TopLevel-Fenster schließen, wenn Klick außerhalb