2015-11-23 3 views
0

Je suis complètement nouveau à Perl/Tk et j'essaie de créer une application qui nécessite plusieurs menus déroulants. J'utilise des menus d'options pour cela. Mais lorsque je change la valeur d'un menu d'options en cours d'exécution, la valeur de toutes les listes déroulantes change pour cette valeur. S'il vous plaît aider dans ceci. Voici le code:Utilisation de plusieurs menus d'options en 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; 
+0

Vous devez indiquer au moins le code que les utilisateurs de la pile peuvent utiliser pour exécuter et reproduire le problème. –

+0

Oui. Comme Chris l'a mentionné ci-dessus, veuillez fournir le code Perl/Tk entier - ou au moins certains qui sont exécutables - afin que nous puissions vous aider. Par exemple, $ symptomFrame n'est pas défini ci-dessus, donc nous ne pouvons pas vraiment exécuter ce que vous nous avez fourni. Merci. – tale852150

+0

Bonjour, j'ai mis à jour le code. Jetez un coup d'oeil s'il vous plait. –

Répondre

1

Lorsqu'une option est sélectionnée dans un widget de Optionmenu, il met à jour le scalaire référencé dans l'option widget -textvariable. En outre, lorsque ce scalaire est modifié, le widget shows the modified text automatically.

Dans le code, tous les widgets Optionmenu ont reçu une référence au même scalaire ($severityText) et par conséquent, ils affichent toujours le même texte. Pour résoudre ce problème, vous pouvez créer une nouvelle variable pour -textvariable de chaque widget, mais vous pouvez vous en sortir en n'utilisant pas du tout l'option.

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

Bien que non lié à la question, vous devriez envisager de créer un nouveau widget composite qui contient les widgets pour un symptôme. Cela facilitera l'ajout de nouveaux symptômes ou changera facilement l'apparence de tous les widgets de symptômes. Voir here pour une documentation à ce sujet.

+0

Merci @srvsh, ça m'a aidé. Je suis coincé sur un autre problème maintenant. J'essaie de passer le champ -variable dans une fonction. Dans une liste déroulante j'ai -variable => \ $ fièvre et j'ai aussi -command => [\ & show_choice, \ $ fever] .. J'essaie d'imprimer la valeur de l'option déroulante sélectionnée. Mais ce que je reçois est quelque chose comme SCALAR (0X765 ...). Comment puis-je obtenir la valeur affichée? J'ai essayé d'utiliser -textVariable au lieu de variable, mais cela fonctionne de la même manière. –