IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques Perl Discussion :

Probleme recuperation de variable


Sujet :

Interfaces Graphiques Perl

  1. #1
    Membre averti
    Femme Profil pro
    Ingénieur informatique scientifique
    Inscrit en
    Mai 2010
    Messages
    313
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur informatique scientifique
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2010
    Messages : 313
    Points : 301
    Points
    301
    Par défaut Probleme recuperation de variable
    Bonjour,
    Je réalise une interface Perl/Tk, celle-ci étant assez longue j'ai séparé mon code en plusieurs modules.
    Je rencontre alors un problème avec mes menus déroulants "optionmenu".

    En effet, lorsque je choisis une option dans mon menu déroulant, je veux afficher un second menu déroulant. Le problème est que je n'arrive pas à récupérer le choix de ce second menu déroulant dans mon programme principal.

    Voici un code exemple de mon problème:

    PROGRAMME PRINCIPAL
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #!/usr/bin/perl
    use Tk;
    use strict;
    use warnings;
    use Module;
     
    ##### Variables #####
    my $choix_op1;
    my $choix_op2;
     
    ##### Creation fenetre principale #####
    my $main_window = MainWindow->new;
    $main_window->minsize(700,540);
     
    ##### Contenu de la fenetre #####
    my $opmenu1 = $main_window->Optionmenu(-options=>["Option1","Option2"],
    									   -textvariable=>\$choix_op1,
    									   -command=>\&afficher)->place(-x=>80,-y=>80);
     
    my $label1 = $main_window->Label(-text=>"MAIN:")->place(-x=>350,-y=>50);
    my $label2 = $main_window->Label(-text=>"Vous avez choisi:")->place(-x=>350,-y=>75);
    my $label3 = $main_window->Label(-textvariable=>\$choix_op1)->place(-x=>350,-y=>100);
    my $label2 = $main_window->Label(-text=>"Vous avez choisi:")->place(-x=>350,-y=>125);
    my $label3 = $main_window->Label(-textvariable=>\$choix_op2)->place(-x=>350,-y=>150);
     
     
    ##### Focntions #####
    sub afficher
    {
    	($choix_op2)=Module::afficher($main_window,$choix_op1);
    }
     
    MainLoop;
    MODULE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use Tk;
    use strict;
    use Class::Struct; 
     
    package Module;
     
    ##### Variables #####
    my $choix_op2;
    my $main_window;
     
    ##### Fonction #####
    ## Fonction appelee dans le main
    sub afficher
    {
    	   $main_window=$_[0];
    	my $option_choisie=$_[1];
     
    	my $label1 = $main_window->Label(-text=>"MODULE:")->place(-x=>200,-y=>50);
    	my $label2 = $main_window->Label(-text=>"Vous avez choisi:")->place(-x=>200,-y=>75);
    	my $label3 = $main_window->Label(-textvariable=>\$option_choisie)->place(-x=>200,-y=>100);
     
    	my $opmenu2 = $main_window->Optionmenu(-options=>["Option3","Option4"],
    									   -textvariable=>\$choix_op2,
    									   -command=>\&afficher_mod)->place(-x=>80,-y=>120);
    	return ($choix_op2);
    }
     
    ## Fonction appelee dans le module
    sub afficher_mod
    {
    		my $label4 = $main_window->Label(-text=>"Vous avez choisi:")->place(-x=>200,-y=>125);
    		my $label5 = $main_window->Label(-textvariable=>\$choix_op2)->place(-x=>200,-y=>150);
    }
     
    1;

    Lorsque on sélectionne l'option 3 ou 4, on a bien le changement dans la partie "module", mais pas dans la partie "Main".
    Quelqu'un pourrait-il m'aider? Merci d'avance.

  2. #2
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Je n'ai pas tout compris. Il faut que tu apprennes à échanger des références entre les procédures. Ça te faciliteras beaucoup la vie.

    Voici ton code que tu peux tester et me dire si ça te convient. Tu pourras l'améliorer.

    Programme principal
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use Tk;
    use strict;
    use warnings;
    use Module;
     
    ##### Variables #####
    my $choix_op1;
    my $choix_op2;
     
    ##### Creation fenetre principale #####
    my $main_window = MainWindow->new;
    $main_window->minsize( 700, 540 );
     
    ##### Contenu de la fenetre #####
    my $opmenu1 = $main_window->Optionmenu(
      -options      => [ "Option1", "Option2" ],
      -textvariable => \$choix_op1,
      -command => [ \&Module::afficher, $main_window, \$choix_op1 ],
    )->place( -x => 80, -y => 80 );
     
    my $label1 = $main_window->Label( -text => "MAIN:" )->place( -x => 350, -y => 50 );
    my $label2 = $main_window->Label( -text => "Vous avez choisi:" )->place( -x => 350, -y => 75 );
    my $label3 = $main_window->Label( -textvariable => \$choix_op1 )->place( -x => 350, -y => 100 );
    my $label4 = $main_window->Label( -text => "Vous avez choisi:" )->place( -x => 350, -y => 125 );
    my $label5 = $main_window->Label( -textvariable => \$choix_op2 )->place( -x => 350, -y => 150 );
     
    MainLoop;
    Module :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package Module;
     
    use warnings;
    use strict;
     
    ##### Fonction #####
    ## Fonction appelee dans le main
    sub afficher {
      my ( $main_window, $ref_choix2 ) = @_;
      my $choix_op2 = ${$ref_choix2};
     
      my $label1 = $main_window->Label( -text => "MODULE:" )->place( -x => 200, -y => 50 );
      my $label2 = $main_window->Label( -text => "Vous avez choisi:" )->place( -x => 200, -y => 75 );
      my $label3 = $main_window->Label( -textvariable => $ref_choix2 )->place( -x => 200, -y => 100 );
     
      my $opmenu2 = $main_window->Optionmenu(
        -options      => [ "Option3", "Option4" ],
        -textvariable => $ref_choix2,
        -command => [ \&afficher_mod, $main_window, \$choix_op2 ],
      )->place( -x => 80, -y => 120 );
      return;
    }
     
    ## Fonction appelee dans le module
    sub afficher_mod {
      my ( $main_window, $ref_choix2 ) = @_;
      my $label4 = $main_window->Label( -text => "Vous avez choisi:" )->place( -x => 200, -y => 125 );
      my $label5 = $main_window->Label( -textvariable => $ref_choix2 )->place( -x => 200, -y => 150 );
    }
     
    1;

  3. #3
    Membre averti
    Femme Profil pro
    Ingénieur informatique scientifique
    Inscrit en
    Mai 2010
    Messages
    313
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur informatique scientifique
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2010
    Messages : 313
    Points : 301
    Points
    301
    Par défaut
    Merci d'avoir répondu aussi vite.
    Avec le code que tu proposes, les deux menus sont sur la même variable, cependant je voudrais deux variables différentes, et récupérer la valeur de la deuxième variable dans mon main.
    Je ne sais pas si je suis très claire...

  4. #4
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Le but de mon code n'est pas de te donner la réponse absolue car je n'ai pas tout saisie de ce que tu souhaites faire. Néanmoins, il te permet de voir comment procéder pour résoudre ton problème. Essaye de comprendre mon code et de l'adapter à tes besoins. En cas de non compréhension, dit le nous.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [FLASH 8] Probléme récupération variable PHP
    Par hedgehog dans le forum Flash
    Réponses: 5
    Dernier message: 31/08/2006, 00h28
  2. [Sql] Probleme de recuperation de variable
    Par loki8 dans le forum Oracle
    Réponses: 13
    Dernier message: 25/07/2006, 14h07
  3. Réponses: 2
    Dernier message: 30/06/2006, 09h07
  4. Réponses: 1
    Dernier message: 22/05/2006, 14h44
  5. [C#] [débutant ]Probleme recuperation variable
    Par p1k1 dans le forum ASP.NET
    Réponses: 9
    Dernier message: 08/03/2006, 12h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo