in reply to Re^3: Gtk2: main window visibility
in thread Gtk2: main window visibility

Ok, here is a simplified version of the program. All it has is a right click menu and a left click interface. The procedure is still the same you have to select VCI from the menu first. The window shows but I cant get it to hide when the user left clicks the system tray. I tried to print the $visible variable and it does not seem to change from 0.
#!/usr/bin/perl -w use strict; use warnings; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use Gtk2::TrayIcon; #global variables my $current_controls = 0; my $visible = 0; #icon goes in box, box goes in tray my $icon = Gtk2::Image->new_from_stock( 'gtk-open', 'menu'); my $eventbox = Gtk2::EventBox->new; $eventbox->add($icon); my $tray = Gtk2::TrayIcon->new('Test'); $tray->add($eventbox); #events and timeouts $eventbox->signal_connect('button_release_event', \&click); #show tray $tray->show_all; #end event loop Gtk2->main; #handles tray clicks sub click { #left mouse button if ($_[1]->button == 1) { #Window show/hide controls if ( $current_controls != 0) { if ( $visible == 0) { $visible = 1 } if ( $visible == 1) { $visible = 0 } &interface; } } #right mouse button elsif ($_[1]->button == 3) { &menu; } return 1; } #right click menu sub menu { my $menu = Gtk2::Menu->new(); #VCI my $menu_VCI = Gtk2::ImageMenuItem->new_with_label("VCI"); $menu_VCI->signal_connect(activate => sub { $current_controls = 1} +); $menu_VCI->set_image(Gtk2::Image->new_from_stock('gtk-refresh', 'm +enu')); $menu->add($menu_VCI); #configure my $menu_pref = Gtk2::ImageMenuItem->new_with_label("Configure"); $menu_pref->set_image(Gtk2::Image->new_from_stock('gtk-preferences +', 'menu')); $menu->add($menu_pref); #separator my $menu_sep = Gtk2::SeparatorMenuItem->new(); $menu->add($menu_sep); #Quit my $menu_quit = Gtk2::ImageMenuItem->new_with_label("Quit"); $menu_quit->signal_connect(activate => sub { Gtk2->main_quit}); $menu_quit->set_image(Gtk2::Image->new_from_stock('gtk-quit', 'men +u')); $menu->add($menu_quit); $menu->show_all; #popup menu, the three is for right mouse button $menu->popup(undef,undef,undef,3,undef,undef); return 1; } #Program interfaces sub interface { my $interface = Gtk2::Window->new('popup'); $interface->set_position('center'); #Eventbox for capturing the mouse click my $eventbox_interface = Gtk2::EventBox->new; #table to contain the widgets my $table_interface = Gtk2::Table->new( 8, 2, FALSE); #menu items for VCI if ( $current_controls == 1) { #Labels my $label_vci_downloading = Gtk2::Label->new( 'Downloading'); my $label_vci_uploading = Gtk2::Label->new( 'Uploading'); #Bold text for the Main Labels my $fontdescription = Gtk2::Pango::FontDescription->new; $fontdescription->set_weight( 'bold'); $label_vci_uploading->modify_font( $fontdescription); $label_vci_downloading->modify_font( $fontdescription); #labels for the progressbars my $label_vci_uasposition = Gtk2::Label->new( 'uasposition'); my $label_vci_waytemp1 = Gtk2::Label->new( 'waytemp'); my $label_vci_waytemp2 = Gtk2::Label->new( 'waytemp'); my $label_vci_wayfinal = Gtk2::Label->new( 'wayfinal'); #Horizontal Separator my $hseparator_vci = Gtk2::HSeparator->new; #Connect button-> will switch to stop after press my $menu_vci_connect = Gtk2::Button->new("Connect"); #Attach the widgets to the table $table_interface->attach_defaults( $label_vci_uploading, 0, 2, + 0, 1); $table_interface->attach_defaults( $label_vci_uasposition, 0, +1, 1, 2); $table_interface->attach_defaults( $label_vci_waytemp1, 0, 1, +2, 3); $table_interface->attach_defaults( $label_vci_wayfinal, 0, 1, +3, 4); $table_interface->attach_defaults( $label_vci_downloading, 0, +2, 4, 5); $table_interface->attach_defaults( $label_vci_waytemp2, 0, 1, +5, 6); $table_interface->attach_defaults( $hseparator_vci, 0, 2, 6, 7 +); $table_interface->attach_defaults( $menu_vci_connect, 1, 2, 7, + 8); #button function $menu_vci_connect->signal_connect( 'button-press-event' => sub + { my $button_vci_label = $menu_vci_connect->get_label; if ( $button_v +ci_label eq 'Connect') { $menu_vci_ +connect->set_label( 'Stop'); } else { $menu_vci_ +connect->set_label( 'Connect'); } }); } #add the widgets to the window: widgets->table->eventbox->window $eventbox_interface->add( $table_interface); $interface->add( $eventbox_interface); $interface->show_all; # if ( $visible == 0) { # $interface->hide_all; # } # else { # $interface->show_all # } }

Replies are listed 'Best First'.
Re^5: Gtk2: main window visibility
by zentara (Cardinal) on Jul 30, 2007 at 20:09 UTC
    That's alot easier to deal with. :-) First, I think you got sent on a wild goose chase with is_visible...... what you want is "is_mapped". I just quickly added some is_mapped and a "$mapped" to replace your $visible. You may as well learn right, and call it "mapped". :-) Also you need to make $interface a global, to access it from different subs. Anyway, here is a quick fix, but it should show you the way. The logic I setup probably could use a better design, and you need to remove your is_visible clutter. Also, you might be able to remove the need for a global $interface.

    Your interface window is probably the place to put your progressbar..... it will keep running in the background when the $interface is not mapped.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      I was thinking today, can I use this program in windows or is there some other module that I should use?
        There is the hassle of installing Gtk2 on windows. You might want to look at the Win32 modules for an alternative. See Statusbar icon "shell" to hide console window? and here is a script I have (I found it somewhere, maybe here). Untested:
        #!/usr/bin/perl use warnings; use strict; #The function name is defined as "Win32::GUI::NotifyIcon". #You will need to be carefull about the order of your sub... #Here a sample script to let's you see, just choose a nice win32 ico a +nd #name it god.ico in the same dir from where you will launch this scrip +t: #--------------- BEGIN{ use Win32::Console; Win32::Console::Free(); } use Win32::GUI; use Tk; $mw = MainWindow -> new; $mw -> wm('geometry', '0x0+0+0'); $mw->overrideredirect(1); &do_win32_stuff; MainLoop; #-------------------------------- sub do_win32_stuff{ $mw_win32 = new Win32::GUI::DialogBox( -width => 0, -height => 0, -name => 'MainWindow'); $icon = new Win32::GUI::Icon('god.ico'); new Win32::GUI::NotifyIcon( $mw_win32, -name => "Notify", -id => 1, -icon => $icon, -tip => "I\'am in the Systray!"); $call = Win32::GUI::Dialog(); $mw_win32->Notify->Delete(-id => 1); sub Notify_Click{ &my_menu; } } #-------------------------------- sub my_menu{ $popup = $mw->Menu(Name => 'popupMenu', -tearoff => 0); $popup->command(-label => 'Number 1',-command => [\&do_label,1] ); $popup->command(-label => 'Number 2',-command => [\&do_label,2]); $popup->separator; $popup->command(-label => 'Number 3', -command => [\&do_label,3]); $popup->command(-label => 'Number 4', -command => [\&do_label,4]); $popup->command(-label => 'Number 5', -command => [\&do_label,5]); $popup->separator; $popup->command(-label => 'Quit', -command => [ \&stop]); $popup->Popup(-popover => 'cursor', -popanchor => 'nw'); } #-------------------------------- sub stop{ exit; } #-------------------------------- sub do_label{ if(Exists($top)){ $label-> configure(-text => "I\'am $_[0]"); } else { $top = $mw ->Toplevel; $top->title(" Numbers"); $top->focus; $label = $top->Label (-text => "I\'am $_[0]", -relief => 'groove', -width => '24')->pack; } }

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum