Thread Entry & Scale via -textvariable: Problem... (15 answers)
Opened by pktm at 2005-08-12 05:16

pktm
 2005-08-12 05:16
#44349 #44349
User since
2003-08-07
2921 Artikel
BenutzerIn
[Homepage]
user image
Hallo!

Ich habe da ein Programm bei dem man in einem Entry-Widget eine Zahl zwischen 0 und 100 eingeben können soll die dann auf einer Scale repräsentiert wird.

Wenn ich die beiden Widgets über das Attribut -textvariable=>\$wert verknüpfe gibt es das Problem, dass das Progtramm abstürzt, wenn der Benutzer etwas anderes als eine  Zahl eingibt.

Fehler:
Quote
Tcl_VarTraceProc returned 'can't assign non-numeric value to scale variable'

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


Leider klappt das Abfangen von invaliden Einagben im Entry-Widget mit Hilfe der Funktionen -validate=>'all', -validatecommand und -invalidCommand nicht.
Vollständiges Testscript:
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
#!/Perl/bin/perl

use strict;
use warnings;
use Tk;
use Tk::Scale;
use Tk::Balloon;

my $mw = Tk::MainWindow->new(-width=>150,-height=>150,);
$mw->gridPropagate(0);
$mw->packPropagate(0);

my $wert = 0;

my $e = $mw->Entry(-textvariable=>\$wert);

my $s = $mw->Scale(
-variable => \$wert,
-from => 0,
-to => 100,
-length => 50,
-showvalue => 0,
-sliderlength => 6,
-sliderrelief => 'raised',
-orient => 'horizontal',
);

$e->configure(
-validate => 'all',
-validatecommand => sub {
if( $wert =~ m/\D/g ) {
$wert =~ s/\D//g;
# invalid-command kommt hier zum Zuge
return 0;
}
return 1;
},
);



$e->pack();
$s->pack();

Tk::MainLoop;


Ich nehme mal an, dass das Problem daher rührt, dass $wert geändert wird, bevor es durch die Validation des Entry-Widgets geschickt wird. Deshalb wird auch versucht die Scale mit einem ungültigen Wert zu füttern.

Gibt es eine Möglichkeit das zu umgehen?
Die Scale hat glaube ich auch so etwas wie ein -validate - Kommando, aber so ganz habe ich das nicht verstanden (bin leider ein Englisch-Banause):
Quote
Name: command

Class: Command

Switch: -command

Specifies the prefix of a perl/Tk callback to invoke whenever the scale's value is changed via a method. The actual command consists of this option followed by a space and a real number indicating the new value of the scale.


> Gibt es eine Möglichkeit das zu umgehen?
Es gäbe natürlich auch die Möglichkeit (glaube ich), die Änderungen über eine Methode zu überwachen und bei gültigen Werten die Scale zu ändern.
Aber das habe ich bisher nicht hinbekommen (ist ja auch schon spät).
Öhm, das hat nicht zufällig schon jemand mal gemacht? :blush:

Grüße, pktm
http://www.intergastro-service.de (mein erstes CMS :) )

View full thread Entry & Scale via -textvariable: Problem...