Das Eingabefeld Tk::Entry

In der Perl/Tk-Distribution enthalten ist Tk::Entry für Eingabefelder. Entrys entsprechen in etwa dem HTML-Element <input>, sind aber etwas mächtiger.

Eine Variable kann mittels der Option -textvariable an das Entry gekoppelt werden. Verändert sich der Wert, wird die Anzeige auf der GUI aktualisiert. Andersherum genauso: wird in der GUI etwas im Entry geschrieben, enthält die Variable diesen Wert.

  1. #!perl
  2. use strict;
  3. use warnings;
  4. use Tk;
  5. my $mw = MainWindow->new();
  6. my $entry = $mw->Entry()->pack();
  7. my $button = $mw->Button(
  8. -text => 'Print entry value',
  9. -command => sub{
  10. my $entry_value = $entry->get();
  11. printf("Entry value: '%s'\n", $entry_value);
  12. },
  13. )->pack();
  14. $mw->MainLoop();
  15. exit(0);

  1. #!perl
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use Tk;
  6. # Creates an entry that you can type in.
  7. # focus puts the cursor in the entry,
  8. # and the button clears it
  9. my $mw = tkinit();
  10. my $name = 'bound to entry';
  11. my $label = $mw->Label(
  12. -text => 'Enter:',
  13. )->grid(
  14. -row => 0,
  15. -column => 0,
  16. );
  17. my $entry = $mw->Entry(
  18. -textvariable => \$name
  19. )->grid(
  20. -row => 0,
  21. -column => 1,
  22. );
  23. my $button = $mw->Button(
  24. -text => 'Clear',
  25. -command => sub{
  26. $name = '';
  27. },
  28. )->grid(
  29. -row => 2,
  30. -column => 0,
  31. -columnspan => 2,
  32. );
  33. $mw->MainLoop();
Top