Schrift
[thread]11122[/thread]

TopLevel-Fenster schließen, wenn Klick außerhalb (Seite 2)

Leser: 2


<< |< 1 2 >| >> 15 Einträge, 2 Seiten
Gast Gast
 2008-01-14 18:15
#104717 #104717
Code: (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
sub del
{
my( $self, @num ) = @_; # pick parameters

$self->close if $self->{container}->ismapped; # close the menu if it is being displayed

# delete menu entries
for my $i (
sort { $b <=> $a } @num # sort this to delete the right entries
) {
$self->{menu}->[$i] -> destroy(); # destroy the menu entry
splice( @{ $self->{menu} }, $i, 1 ); # delete the array member
} # for

return $self; # return object
} # del

sub close
{
my( $self ) = @_; # pick parameters

$self->{shadow} -> withdraw; # close shadow
$self->{container} -> withdraw; # close menu

return $self; # return object
} # close

sub show
{
my( $self, $x, $y ) = @_; # pick parameters

$self->close if $self->{container}->ismapped; # close menu if it is being displayed

# set shadow position
$self->{shadow} -> geometry(
'=' .
$self->{container}->reqwidth . # menu width
'x' .
$self->{container}->reqheight . # menu height
'+' .
( $x + 3 ) . # menu x position + extra pixels for the shadow
'+' .
( $y + 3 ) # menu y position + extra pixels for the shadow
);
$self->{shadow} -> deiconify; # show shadow

$self->{container} -> geometry("+$x+$y"); # set menu position
$self->{container} -> deiconify; # show menu
$self->{container} -> raise( $self->{shadow} ); # draw the menu over the shadow
$self->{container} -> focus(); # give focus() to the menu

return $self; # return object
} # show

1;


Und ein kleines Beispiel wie man diese Klasse anwendet:
Gast Gast
 2008-01-14 18:18
#104718 #104718
Code: (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
#!/usr/bin/perl

use strict;
use warnings 'all';

use Tk;
use Tk::KMenu;

# create main window
my $mw = tkinit();

# create menu
my $menu = new Tk::KMenu $mw;

# add menu entries
$menu -> add(
Test => sub {
print "Test\n";
},
Test2 => sub {
print "Test 2\n";
},
Delete => sub {
$menu -> del( 0 .. 2 );
},
Beenden => sub {
$mw -> destroy;
},
);

# should create a package for MenuButtons, so that the following can be written simpler
#
# create menubutton
#
my $menubutton = $mw -> Button(
-text => 'Open Menu',
) -> pack;

# have to split this because $menubutton has to be defined,
# so we can use it in the -command callback.
$menubutton -> configure(
-command => sub {
$menu->show(
$mw->rootx + $menubutton->x, # calculate the x position
$mw->rooty + $menubutton->y # calculate the y position
+ $menubutton->reqheight,
);
},
);

MainLoop;


MfG Horst
ptk
 2008-01-14 23:58
#104724 #104724
User since
2003-11-28
3645 Artikel
ModeratorIn
[default_avatar]
Typischerweise macht man ein grabGlobal, dann kann man alle Mausklicks abfangen. Aber Vorsicht mit global grabs, da kann man sich bei Programmierfehlern leicht aussperren!
Gast Gast
 2008-01-15 09:41
#104730 #104730
Hey! Das sieht ja geil aus ;) Danke Horst!

Sollten wir uns mal begegnen, gebe ich garantiert ein aus.

Viele Grüße
Alex
Gast Gast
 2008-01-17 16:18
#104839 #104839
Gast+2008-01-15 08:41:16--
Hey! Das sieht ja geil aus ;) Danke Horst!

Kein Problem macht ja auch Spaß :)

Gast+2008-01-15 08:41:16--
Sollten wir uns mal begegnen, gebe ich garantiert ein aus.

Da komm ich gerne drauf zurück ;)

MfG Horst
<< |< 1 2 >| >> 15 Einträge, 2 Seiten



View all threads created 2008-01-10 15:32.