Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]5314[/thread]

Anfrage an die FAQ: Grundsätzliches TK-Problem



<< |< 1 2 >| >> 14 Einträge, 2 Seiten
JW
 2007-05-18 01:52
#46325 #46325
User since
2003-08-04
467 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Folgenden Anfrage kam per Mail an die FAQ-Abteilung. ;-) Da ich mit Tk kaum bis keinerlei Erfahrung habe, leite ich mal ins Forum weiter. E-Mail Adresse des Hilfsbedürftigen gibt es bei mir. Ich gebe ihm aber auch den Link diese Fred's.

JW


Hallo!

Ich finde eure Page wirklich sehr hilfreich, aber ich habe leider ein Problem, da ich bis Montag ein Perl/Tk-Beispiel zu programmieren habe und leider nicht weiter weiß.

Hier ist mein Programm, dass ich bis jetzt geschrieben habe und es funktioniert tadellos:

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
56
#!/usr/bin/perl 

use strict;
use warnings;
use Tk;


# Hauptfenster
my $nw = MainWindow->new();

# Titel
$nw->title ('Man-Machine Interfaces II - UE - BSP 1');

# Menü
my $mb = $nw->Frame (-relief => 'ridge')->pack (-side => 'top', -fill => 'x');
my $m_file = $mb->Menubutton (-text => "File", -underline => 0)->pack (-side => 'left');
$m_file->command (-label => "Activate", -command => sub {activate()});
$m_file->separator();
$m_file->command (-label => "Quit", -command => [$nw => 'destroy']);

# Frames
my $f1 = $nw->Frame()->pack (-side => 'top', -expand => 1, -fill => 'both');
my $f2 = $nw->Frame()->pack (-side => 'bottom', -expand => 1, -fill => 'both');

# Radiobuttons
my $r1 = $f1->Frame (-borderwidth => 3, -relief => 'groove')->pack (-side => 'left', -expand => 1, -fill => 'both');
my @listenwahl = ('1', '2', '3');
my $liste = 0;
for my $a (0..$#listenwahl)
{
$r1->Radiobutton (-text => $listenwahl[$a], -variable => \$liste, -value => $a)->pack (-anchor => 'e');
}

my $r2 = $f1->Frame (-borderwidth => 3, -relief => 'groove')->pack (-side => 'left', -expand => 1, -fill => 'both');
my @textwahl = ('normal', 'rückwärts');
my $text = 0;
for my $b (0..$#textwahl)
{
$r2->Radiobutton (-text => $textwahl[$b], -variable => \$text, -value => $b)->pack (-anchor => 'w');
}

# Entries
my $entry1 = $nw->Entry (-text => "Das ist ein Satz!")->pack (-side => 'top');
my $entry2 = $nw->Entry (-text => "!ztaS nie tsi saD");

# Listboxen und Scrollbars
my $listbox1 = $f2->Listbox()->pack (-side => 'left', -expand => 1, -fill => 'both');
my $scrollbar1 = $f2->Scrollbar(-command, [yview => $listbox1])->pack (-side => 'left', -expand => 1, -fill => 'both');

my $listbox2 = $f2->Listbox()->pack (-side => 'left', -expand => 1, -fill => 'both');
my $scrollbar2 = $f2->Scrollbar(-command, [yview => $listbox2])->pack (-side => 'left', -expand => 1, -fill => 'both');

my $listbox3 = $f2->Listbox()->pack (-side => 'left', -expand => 1, -fill => 'both');
my $scrollbar3 = $f2->Scrollbar(-command, [yview => $listbox3])->pack (-side => 'left', -expand => 1, -fill => 'both');

MainLoop();


Wie man sieht, hab ich beim Menü anlegen "Activate" angelegt und darin subroutine "activate". Jetzt soll mit dieser subroutine folgendes passieren.

Die ersten 3 Radiobuttons geben an, welche Listbox ausgewählt wird und die anderen 2 geben an, die der Entry-Text in der jeweiligen Listbox angezeigt werden soll (entry2 soll als entry nicht angezeigt werden).

Wenn Radiobutton 1 und Radiobutton normal angeklickt sind, dann soll in der Listbox 1 der Text normal angezeigt werden. Wenn Radiobutton 1 und Radiobutton rückwärts angeklickt sind, dann soll in der Listbox 1 der Text rückwärts angezeigt werden (daher entry2). Für Radiobutton 2 & 3 und Listbox 2 & 3 gilt das Selbe.

Ich weiß, dass ich die Subroutine nach der MainLoop zu schreiben habe. Nur weiß ich leider nicht, wie ich die Radiobuttons miteinander so verknüpfen soll, so dass dann der Text, wenn man im Menü auf Activate klickt, richtig angezeigt wird (entweder normal oder rückwärts).

Ich bedanke mich schon mal im Voraus für die große Hilfe, die ihr mir geben könnt. Dann kann ich am kommenden Montag, das Beispiel ohne Probleme abgeben.

Einen schönen Feiertag.

LG, Robert!
Gast Gast
 2007-05-18 13:34
#46326 #46326
Hoi, hab dir das mal umgebaut, bin aber leider gerad in Eile und kann dir keine erklaerung dazu geben, sorry..

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
56
57
58
59
60
61
62
63
#!/usr/bin/perl

use strict;
use warnings;
use Tk;


# Hauptfenster
my $nw = MainWindow->new();

# Titel
$nw->title ('Man-Machine Interfaces II - UE - BSP 1');

# Menü
my $mb = $nw->Frame (-relief => 'ridge')->pack (-side => 'top', -fill => 'x');

my @listboxes;
my $liste = 0;
my $m_file = $mb->Menubutton (-text => "File", -underline => 0)->pack (-side => 'left');
$m_file->command (-label => "Activate", -command => sub {activate($liste,\@listboxes)});
$m_file->separator();
$m_file->command (-label => "Quit", -command => [$nw => 'destroy']);

# Frames
my $f1 = $nw->Frame()->pack (-side => 'top', -expand => 1, -fill => 'both');
my $f2 = $nw->Frame()->pack (-side => 'bottom', -expand => 1, -fill => 'both');

# Radiobuttons
my $r1 = $f1->Frame (-borderwidth => 3, -relief => 'groove')->pack (-side => 'left', -expand => 1, -fill => 'both');
my @listenwahl = ('1', '2', '3');
for my $a (0..$#listenwahl)
{
$r1->Radiobutton (-text => $listenwahl[$a], -variable => \$liste, -value => $a)->pack (-anchor => 'e');
}

my $r2 = $f1->Frame (-borderwidth => 3, -relief => 'groove')->pack (-side => 'left', -expand => 1, -fill => 'both');
my @textwahl = ('normal', 'rückwärts');
my $text = 0;
for my $b (0..$#textwahl)
{
$r2->Radiobutton (-text => $textwahl[$b], -variable => \$text, -value => $textwahl[$b])->pack (-anchor => 'w');
}

# Entries
my $entry1 = $nw->Entry (-text => "Das ist ein Satz!")->pack (-side => 'top');
my $entry2 = $nw->Entry (-text => "!ztaS nie tsi saD");

for (0..2) {
push(@listboxes, $f2->Listbox()->pack (-side => 'left', -expand => 1, -fill => 'both'));
$f2->Scrollbar(-command, [yview => $listboxes[$_]])->pack (-side => 'left', -expand => 1, -fill => 'both');
}

MainLoop();

sub activate {
my ($liste, $listbox_ref) = @_;
print "$liste\n";
print join("\n", @$listbox_ref)."\n";
my $string = $entry1->get();
my @string = reverse split//,$string;
$string = reverse($string) if ($text eq 'rückwärts');
@$listbox_ref[$liste]->insert('0.0', $string);
}
Gast Gast
 2007-05-18 13:38
#46327 #46327
ups.. in der activate(); hab ich wohl nen paar test ueberig gelassen..

die Zeilen koennen da weg =)
my @string = reverse split//,$string;
print "$liste\n";
print join("\n", @$listbox_ref)."\n";

MfG
Gast Gast
 2007-05-18 15:08
#46328 #46328
danke dir für die hilfe. werde das am abend austesten. ;)
Gast Gast
 2007-05-19 17:15
#46329 #46329
ich hab mir nun das ganze angesehen und es funktioniert wunderbar. nur eine frage hab ich noch. der text vom entry soll gesplittet sein, sprich, dass es so aussieht:

"
Das
ist
ein
Satz
!
"

wie mache ich das bzw. wo muss ich das hinschreiben, damit das geschieht und wie kann ich den eingegebenen text in der listbox dann wieder löschen?

danke nochmals für die nützliche info. ;)
styx-cc
 2007-05-19 17:33
#46330 #46330
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Damit er dir das splittet und untereinander anzeigt kannste die activate(); wie folgt umbauen:

Code: (dl )
1
2
3
4
5
6
7
8
sub activate {
my ($liste, $listbox_ref) = @_;

my $string = $entry1->get();
$string = reverse($string) if ($text eq 'rückwärts');
my @string = split/ /, $string;
@$listbox_ref[$liste]->insert('end', "$_") for (@string);
}


und um eine Listbox zu leeren nimmste:
Code: (dl )
$listbox->delete(index1,index2)

vielleicht geht auch:
Code: (dl )
$listbox->deleteAll();

Steht aber auch alles in der Tk::Listbox - Doku : -)

MfG
Pörl.
Gast Gast
 2007-05-19 18:20
#46331 #46331
danke nochmals für die Hilfe. Beim Splitten tritt aber folgendes Problem auf. Es wird nämlich nur

"
Das
"

anstatt

"
Das
ist
ein
Satz
!
"

angezeigt.

Bitte nochmals um Hilfe. ;)
Gast Gast
 2007-05-19 18:24
#46332 #46332
Hab's schon gelöst. Hab nur eine falsche Zahl beim deleten hingetan. ;) Danke nochmals. ;)
Gast Gast
 2007-05-19 18:28
#46333 #46333
Sorry noch ein Problem!

Die Ausgabe ist nun folgende:

"Das
ist
ein
Satz
!
"

Aber sobald ich in die Listbox auf Rückwärts gehe, dann schaut's so aus:

"
Das
ist
ein
Satz
!
!
ztaS
"

und weiter geht's nicht. was kann da sein? ;)
Gast Gast
 2007-05-19 18:43
#46334 #46334
Ich hab's nun hinbekommen. Es funktioniert, so wie es sein soll. Doch wenn ich mehrere Sachen in eine Listbox einfüge, dann kann man nur eine Zeile hinunterscrollen und weiter nicht. Was könnte denn da der Fehler sein? ;)
<< |< 1 2 >| >> 14 Einträge, 2 Seiten



View all threads created 2007-05-18 01:52.