在Perl / Tk中使用多个选项菜单

时间:2015-11-23 04:27:04

标签: perl tk perltk

我对Perl / Tk完全不熟悉并尝试制作需要多个下拉菜单的应用程序。我正在使用选项菜单。但是当我在运行时更改一个选项菜单的值时,所有下拉菜单的值都会更改为此值。请帮忙。这是代码:

    #!/usr/bin/perl
use warnings;
use Tk;

my $mw = MainWindow->new;
$mw->geometry("700x700");
$mw->title("AIR (Auto Immune Research)");

#create own title font
$mw->fontCreate("sectionTitleFont", -family => "Helvetica", -size => 36, -weight => "bold");

my $symptomFrame = $mw->Frame(-background => 'white', -foreground => 'black')->pack(-side => "top", -fill => "x");
my $pathologyFrame = $mw->Frame(-background => 'white', -foreground => 'black')->pack(-side => "bottom", -fill => "x");

my $symptomLabel = $symptomFrame->Label(-background => 'white', -text => 'Patient Symptoms', -font => 'sectionTitleFont');
my $pathologyLabel = $pathologyFrame->Label(-background => 'white', -text => 'Pathological Findings', -font => 'sectionTitleFont');

my $severityText = $symptomFrame->Label(-background => 'white', -text => 'Severity on a scale of 1 to 10');

#cough
my $coughCheckBox = $symptomFrame->Checkbutton(-text => 'Cough', -background => 'white');
my $coughSeverity;
my $coughSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$coughSeverity,
-textvariable => \$severityText
);

#Fever
my $feverCheckBox = $symptomFrame->Checkbutton(-text => 'Fever', -background => 'white');
my $feverSeverity;
my $feverSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$feverSeverity,
-textvariable => \$severityText
);

#joint pain
my $jointPainCheckBox = $symptomFrame->Checkbutton(-text => 'Joint Pain', -background => 'white');
my $jointPainSeverity;
my $jointPainSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$jointPainSeverity,
-textvariable => \$severityText
);

#Moon face
my $moonFaceCheckBox = $symptomFrame->Checkbutton(-text => 'Moon Face', -background => 'white');
my $moonFaceSeverity;
my $moonFaceSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$moonFaceSeverity,
-textvariable => \$severityText
);

#fatigue
my $fatigueBox = $symptomFrame->Checkbutton(-text => 'Fatigue', -background => 'white');
my $fatigueSeverity;
my $fatigueSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$fatigueSeverity,
-textvariable => \$severityText
);

#Skin Redness
my $skinRednessBox = $symptomFrame->Checkbutton(-text => 'Skin Redness', -background => 'white');
my $skinRednessSeverity;
my $skinRednessSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$skinRednessSeverity,
-textvariable => \$severityText
);

#Drowsiness
my $drowsinessBox = $symptomFrame->Checkbutton(-text => 'Drowsiness', -background => 'white');
my $drowsinessSeverity;
my $drowsinessSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$drowsinessSeverity,
-textvariable => \$severityText
);

#Headache
my $headacheBox = $symptomFrame->Checkbutton(-text => 'Headache', -background => 'white');
my $headacheSeverity;
my $headacheSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$headacheSeverity,
-textvariable => \$severityText
);


#Inflamations
my $inflamationsBox = $symptomFrame->Checkbutton(-text => 'Inflamations', -background => 'white');
my $inflamationsSeverity;
my $inflamationsSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$inflamationsSeverity,
-textvariable => \$severityText
);

#Itchiness
my $itchinessBox = $symptomFrame->Checkbutton(-text => 'Itchiness', -background => 'white');
my $itchinessSeverity;
my $itchinessSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$itchinessSeverity,
-textvariable => \$severityText
);

#Blood in Urine
my $bloodBox = $symptomFrame->Checkbutton(-text => 'Blood in Urine', -background => 'white');
my $bloodSeverity;
my $bloodSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$bloodSeverity,
-textvariable => \$severityText
);

#Depression
my $depressionBox = $symptomFrame->Checkbutton(-text => 'Depression', -background => 'white');
my $depressionSeverity;
my $depressionSeverityDD = $symptomFrame->Optionmenu(
-options => [[1=>1], [2=>2], [3=>3], [4=>4], [5=>5], [6=>6], [7=>7], [8=>8], [9=>9], [10=>10]],
-variable => \$depressionSeverity,
-textvariable => \$severityText
);


#emty label to give empty space
my $emptyLabel = $symptomFrame->Label(-background => 'white', -width => '20');
$symptomLabel->grid(-columnspan => '5');
$pathologyLabel->grid(-columnspan => '5');
#$severityText->grid;
$coughCheckBox -> grid($coughSeverityDD, $emptyLabel, $feverCheckBox, $feverSeverityDD);
$jointPainCheckBox -> grid($moonFaceSeverityDD, $emptyLabel, $depressionBox, $depressionSeverityDD);
$moonFaceCheckBox -> grid($jointPainSeverityDD, $emptyLabel, $bloodBox, $bloodSeverityDD);


MainLoop;

1 个答案:

答案 0 :(得分:1)

Optionmenu窗口小部件中选择一个选项后,它会更新-textvariable窗口小部件选项中引用的标量。此外,在修改该标量时,小部件shows the modified text automatically

在代码中,所有Optionmenu窗口小部件都已传递对同一标量($severityText)的引用,因此,所有这些窗口小部件总是最终显示相同的文本。要解决此问题,您可以为每个小部件的-textvariable创建一个新变量,但您可以完全不使用该选项。

my $coughSeverity;
my $coughSeverityDD = $symptomFrame->Optionmenu(
    -options => [ 1..10 ],      # you can omit the labels if they are the same as the values
    -variable => \$coughSeverity,
);

虽然与问题无关,但您应该考虑创建一个包含症状窗口小部件的新复合窗口小部件。这样可以更轻松地添加新症状或轻松更改所有症状小部件的外观。有关该文档的一些文档,请参阅here