http://qs321.pair.com?node_id=564740

lepetitalbert has asked for the wisdom of the Perl Monks concerning the following question:

Hello Dear Monks,

I'm trying to create my first GUI and don't understand the following :

use strict; use warnings; use Tk; use Tk::DialogBox; my $main_window = MainWindow -> new(); $main_window -> bind ( "<Control-Key-c>" , \&add_client ) ; # Ctrl c MainLoop; sub add_client { my $add_client_win = $main_window -> DialogBox ( -title => 'Ajouter un client' , -buttons => [ 'Valider' , 'Annuler' ] ); # BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE #my $firstname_line = $add_client_win -> add ( 'Label' , # -height => '30' #) -> pack( # -side => 'top' , # -fill => 'x' #); # END BUG END BUG END BUG END BUG END BUG END BUG END BUG END BUG # -- CANTON -- my $canton_line = $add_client_win -> add ( 'Label' , -height => '30' ) -> pack( -side => 'top' , -fill => 'x' ); my $label_canton = $canton_line -> Label ( -font => 'Helvetica 10 bold' , -text => 'Localisation :', -padx => '10' , -pady => '5' ) -> pack ( -side => 'left' ); my ($canton_data, $canton_display ); my $canton_choice = $canton_line -> Optionmenu( -options => [ [ 'Genève' => '000' ] , [ 'Vaud' => '100' ] , [ ' - Chablais vaudois' => '110' ] , [ ' - Lausanne' => '120' ] , [ ' - Montreux-Vevey' => '130' ] , [ ' - Morges' => '140' ] , [ ' - Nyon' => '150' ] , [ ' - Yverdon' => '160' ] , [ ' - Arc lémanique' => '170' ] , [ 'Valais' => '200' ] , [ ' - Chablais valaisan' => '210' ] , [ ' - Bas Valais' => '220' ] , [ ' - Haut Valais' => '230' ] , [ 'Fribourg' => '400' ] , [ 'France voisine' => '500' ] , [ 'Paris' => '600' ] , [ 'Cote d Azur' => '700' ] , [ 'Italie' => '800' ] , ] , -command => sub { } , -variable => \$canton_data , -textvariable => \$canton_display ) -> pack( -side => 'right' , ); my $reponse = $add_client_win -> Show ( ) ; }

This code works, but when I uncomment the lines between BUG HERE and END BUG, the -text option in my $label_canton disappears.

If I minimize the window or hide it with another window from another app and restore it the text is here again.

And I don't understand WHY

Thanks.

Have a nice day<>/p

P.S Ctrl-c to see the dialogbox

complete code here

"There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Replies are listed 'Best First'.
Re: tk label text disappears ?!
by jdtoronto (Prior) on Jul 31, 2006 at 13:59 UTC
    It's all about packing order. You were placing the labels over the top of each other. Try this:
    #!/usr/bin/perl -w use strict; use strict; use warnings; use Tk; use Tk::DialogBox; my $main_window = MainWindow -> new(); $main_window -> bind ( "<Control-Key-c>" , \&add_client ) ; # Ctrl c MainLoop; sub add_client { my $add_client_win = $main_window -> DialogBox ( -title => 'Ajouter un client' , -buttons => [ 'Valider' , 'Annuler' ] ); # BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE BUG HERE my $firstname_line = $add_client_win -> add ( 'Label' , -height => '5', -text => 'Some text', -font => 'Helvetica 10 bold' , ) -> grid( #-side => 'top' , #-fill => 'x' ); # END BUG END BUG END BUG END BUG END BUG END BUG END BUG END BUG # -- CANTON -- my $canton_line = $add_client_win -> add ( 'Label' , -height => '5' ) -> grid( #-side => 'top' , #-fill => 'x' ); my $label_canton = $canton_line -> Label ( -font => 'Helvetica 10 bold' , -text => 'Localisation :', -padx => '10' , -pady => '5' ) -> pack ( -side => 'left' ); my ($canton_data, $canton_display ); my $canton_choice = $canton_line -> Optionmenu( -options => [ [ 'Genève' => '000' ] , [ 'Vaud' => '100' ] , [ ' - Chablais vaudois' => '110' ] , [ ' - Lausanne' => '120' ] , [ ' - Montreux-Vevey' => '130' ] , [ ' - Morges' => '140' ] , [ ' - Nyon' => '150' ] , [ ' - Yverdon' => '160' ] , [ ' - Arc lémanique' => '170' ] , [ 'Valais' => '200' ] , [ ' - Chablais valaisan' => '210' ] , [ ' - Bas Valais' => '220' ] , [ ' - Haut Valais' => '230' ] , [ 'Fribourg' => '400' ] , [ 'France voisine' => '500' ] , [ 'Paris' => '600' ] , [ 'Cote d Azur' => '700' ] , [ 'Italie' => '800' ] , ] , -command => sub { } , -variable => \$canton_data , -textvariable => \$canton_display ) -> pack( -side => 'right' , ); my $reponse = $add_client_win -> Show ( ) ; }
    By using the  grid geometry manager they are placed vertically in order in a grid.

    jdtoronto

      Hi jdtoronto,

      THANK YOU.

      I somehow totally missed this grid method.

      Have a nice day.

      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates
Re: tk label text disappears ?!
by wulvrine (Friar) on Jul 31, 2006 at 14:50 UTC
    Hi lepetitalbert
    While using grid may solve the actual problem at hand. what I find more interesting is why when running your original code just MOVING the window first, causes it to work properly?

    There seems to be a failed or missing repaint of that text label on the second time. (Which is the first time you've done a selection on the Option Menu.)
    I wonder if this is a bug in the TK code itself? When liverpole and I deleted some items from the OptionMenu the bug also went away. But when we forced a repaint (covering the window, moving it) as you said, the system works as expected.

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|
      wulvrine,

      I think the problem is related to the packing order and parameters. I didnt have time to go into great detail, but I think the two labels were being positioned one on top of the other.

      jdtoronto

        Hi wulvrine,

        while trying to make a minimal code to post, I had strange results like (I'm home now and don't have the codes but I'll check that tomorrow!).

        - the same code but not in a dialog box works

        - if I comment out any item (from the snippet) in $canton_choice it works.

        Now stranger :

        - I tested my unchanged script on my linux box and it works

        - It even works in my XP in vmware.

        ??

        Have a nice day.

        "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates
Re: tk label text disappears ?!
by wulvrine (Friar) on Jul 31, 2006 at 18:04 UTC
    jdtoronto

    I had thought that myself. I put different colored backgrounds for each of the peices in hopes to see this, but it didnt show up. At least not with this method. I'm glad the OP's question was answered, it is hard to let go untill you know 'why' eh? :)

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|