在 Perl/Tk 中使用多个选项菜单
Using multiple Option menus in Perl/Tk
我是 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;
当在 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。
我是 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;
当在 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。