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

I cant seem to figure this out and am wondering if anyone has any ideas. The user should do this: 1) right click the system tray icon and select VCI. 2) Left click the system tray icon. The interface should become visible. 3) Click the "Connect" button. It shouldthen turn to "Stop". 4) Left click the system tray icon and the window will 'hide'. I cant get the program to hide the VCI window and become visible after a left click and be in the same state as it was before. The "Connect" button does not stay as "Stop". Any ideas? The clicked and interface subroutines must be the involved ones.
#!/usr/bin/perl -w use strict; use warnings; use threads; use threads::shared; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use Gtk2::TrayIcon; use Net::SFTP::Foreign; #user variables my $icon_main = Gtk2::Gdk::Pixbuf->new_from_file("/home/deadpickle/Des +ktop/Gtk2/test.png"); my $icon_nav = Gtk2::Gdk::Pixbuf->new_from_file("/home/deadpickle/Desk +top/Gtk2/uvnav.png"); my $icon_vci = Gtk2::Gdk::Pixbuf->new_from_file("/home/deadpickle/Desk +top/Gtk2/vci.png"); my $time = 1200000; #global variables my $user : shared = 'uas'; my $server : shared = 'updraft.unl.edu'; my $local_waypoint : shared = '/local'; my $remote_waypoint : shared = '/remote'; my $waytemp : shared = 'waytemp'; my $wayfinal : shared = 'wayfinal'; my $file = 'default.cfg'; my $local_uaspos : shared = '/local'; my $remote_uaspos : shared = '/remote'; my $local_uas : shared = '/local'; my $remote_uas : shared = '/remote'; my $die : shared = 0; my $sftp_connect : shared = 0; my $current_controls = 0; my $visible = 0; #icon goes in box, box goes in tray my $icon = Gtk2::Image->new_from_pixbuf($icon_main); my $eventbox = Gtk2::EventBox->new; $eventbox->add($icon); my $tray = Gtk2::TrayIcon->new('Test'); $tray->add($eventbox); #threads my $thread_1 = threads->new( \&sftp); #tooltip my $tooltip = Gtk2::Tooltips->new; $tooltip->set_tip($tray, "GRRUVI v1.0"); #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) { #&interface; if ( $visible == 0) { $visible = 1 } if ( $visible == 1) { $visible = 0 } } } #right mouse button elsif ($_[1]->button == 3) { &menu; } return 1; } #right click menu sub menu { my $menu = Gtk2::Menu->new(); #UVNav # my $menu_UVNav = Gtk2::ImageMenuItem->new_with_label("UVNav"); # $menu_UVNav->signal_connect(activate => \&UVNav); # $menu_UVNav->set_image(Gtk2::Image->new_from_stock('gtk-refresh', + 'menu')); # $menu->add($menu_UVNav); #VCI my $menu_VCI = Gtk2::ImageMenuItem->new_with_label("VCI"); $menu_VCI->signal_connect(activate => sub { #change the icon when VCI +is clicked in the menu $eventbox->remove($icon); $icon = Gtk2::Image->new_f +rom_pixbuf($icon_vci); $eventbox->add($icon); $eventbox->reparent($tray) +; $tray->show_all; $tooltip->set_tip($tray, " +Virtual Cockpit Interface"); #Set the correct interface $current_controls = 1; #Display the window on cli +cking #&interface; } ); $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->signal_connect(activate => \&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 => \&exit_threads); $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'); } }); #my $button_vci_label = $menu_vci_connect->get_label; #if ( $button_vci_label eq 'Connect') { # $menu_vci_connect->signal_connect( 'clicked' => sub { $me +nu_vci_connect->set_label( 'Stop')} ); #} #if ( $button_vci_label eq 'Stop') { # $menu_vci_connect->signal_connect( 'clicked' => sub { $me +nu_vci_connect->set_label( 'Connect')} ); #} } #add the widgets to the window: widgets->table->eventbox->window $eventbox_interface->add( $table_interface); $interface->add( $eventbox_interface); Glib::Timeout->add( 100, sub { if ( $visible == 0) { $interface->show_all } else { $interface->hide_all } }); #$interface->show_all; #$interface->signal_connect( 'button-release-event' => sub { $inte +rface->hide_all}); } #configuration dialog window sub configure { #Create the new window my $config_window = Gtk2::Window->new('toplevel'); $config_window->set_title("Configuration"); $config_window->set_position('center'); #VBox container my $table_config = Gtk2::Table->new(3, 5, FALSE); #Create Notebook my $config_notebook = Gtk2::Notebook->new; $config_notebook->set_tab_pos('top'); #the First Page; Config file select my $hbox_file = Gtk2::HBox->new(FALSE, 1); my $label_file = Gtk2::Label->new('Configuration Filename'); my $entry_file = Gtk2::Entry->new; my $button_file = Gtk2::Button->new_with_mnemonic("_Browse"); $button_file->set_size_request( 80, 32); my $align_button_file = Gtk2::Alignment->new( 0.5, 0.5, 0, 0); $align_button_file->add( $button_file); $entry_file->set_text($file); $hbox_file->pack_start($label_file, FALSE, FALSE, 1); $hbox_file->pack_start($entry_file, FALSE, FALSE, 1); $hbox_file->pack_start($align_button_file, FALSE, FALSE, 1); #the Second Page; Connection my $connect_vbox = Gtk2::VBox->new( FALSE, 1); my $label_user = Gtk2::Label->new('Username'); my $entry_user = Gtk2::Entry->new; my $label_server = Gtk2::Label->new('Server'); my $entry_server = Gtk2::Entry->new; $entry_user->set_text($user); $entry_server->set_text($server); $connect_vbox->pack_start($label_user, FALSE, FALSE, 1); $connect_vbox->pack_start($entry_user, FALSE, FALSE, 1); $connect_vbox->pack_start($label_server, FALSE, FALSE, 1); $connect_vbox->pack_start($entry_server, FALSE, FALSE, 1); #the Third Page; Waypoint my $table_waypoint = Gtk2::Table->new(4, 2, FALSE); my $label_temp_waypoint = Gtk2::Label->new('Temp Waypoint Filename +'); my $entry_temp_waypoint = Gtk2::Entry->new; my $label_final_waypoint = Gtk2::Label->new('Final Waypoint Filena +me'); my $entry_final_waypoint = Gtk2::Entry->new; my $label_local_waypoint = Gtk2::Label->new('Local Waypoint Folder +'); my $entry_local_waypoint = Gtk2::Entry->new; my $label_remote_waypoint = Gtk2::Label->new('Remote Waypoint Fold +er'); my $entry_remote_waypoint = Gtk2::Entry->new; $entry_temp_waypoint->set_text($waytemp); $entry_final_waypoint->set_text($wayfinal); $entry_local_waypoint->set_text($local_waypoint); $entry_remote_waypoint->set_text($remote_waypoint); $entry_temp_waypoint->set_editable(0); $entry_final_waypoint->set_editable(0); $table_waypoint->attach_defaults($label_temp_waypoint, 0, 1, 0, 1) +; $table_waypoint->attach_defaults($entry_temp_waypoint, 1, 2, 0, 1) +; $table_waypoint->attach_defaults($label_final_waypoint, 0, 1, 1, 2 +); $table_waypoint->attach_defaults($entry_final_waypoint, 1, 2, 1, 2 +); $table_waypoint->attach_defaults($label_local_waypoint, 0, 1, 2, 3 +); $table_waypoint->attach_defaults($entry_local_waypoint, 1, 2, 2, 3 +); $table_waypoint->attach_defaults($label_remote_waypoint, 0, 1, 3, +4); $table_waypoint->attach_defaults($entry_remote_waypoint, 1, 2, 3, +4); #the Fourth Page; UAS my $vbox_uas = Gtk2::VBox->new( FALSE, 1); my $table_uaspos = Gtk2::Table->new(4, 2, FALSE); my $frame_uaspos = Gtk2::Frame->new( 'uasposition File Transfer'); my $label_local_uaspos = Gtk2::Label->new('Local uasposition Folde +r'); my $entry_local_uaspos = Gtk2::Entry->new; my $label_remote_uaspos = Gtk2::Label->new('Remote uasposition Fol +der'); my $entry_remote_uaspos = Gtk2::Entry->new; $entry_local_uaspos->set_text($local_uaspos); $entry_remote_uaspos->set_text($remote_uaspos); $table_uaspos->attach_defaults($label_local_uaspos, 0, 1, 0, 1); $table_uaspos->attach_defaults($entry_local_uaspos, 1, 2, 0, 1); $table_uaspos->attach_defaults($label_remote_uaspos, 0, 1, 1, 2); $table_uaspos->attach_defaults($entry_remote_uaspos, 1, 2, 1, 2); $frame_uaspos->add( $table_uaspos); my $table_uas = Gtk2::Table->new(4, 2, FALSE); my $frame_uas = Gtk2::Frame->new( 'UAS Placefile Transfer'); my $label_local_uas = Gtk2::Label->new('Local uaspf.txt Folder'); my $entry_local_uas = Gtk2::Entry->new; my $label_remote_uas = Gtk2::Label->new('Remote uaspf.txt Folder') +; my $entry_remote_uas = Gtk2::Entry->new; $entry_local_uas->set_text($local_uas); $entry_remote_uas->set_text($remote_uas); $table_uas->attach_defaults($label_local_uas, 0, 1, 0, 1); $table_uas->attach_defaults($entry_local_uas, 1, 2, 0, 1); $table_uas->attach_defaults($label_remote_uas, 0, 1, 1, 2); $table_uas->attach_defaults($entry_remote_uas, 1, 2, 1, 2); $frame_uas->add( $table_uas); $vbox_uas->pack_start($frame_uaspos, FALSE, FALSE, 1); $vbox_uas->pack_start($frame_uas, FALSE, FALSE, 1); #append pages $config_notebook->append_page($hbox_file, "Configuration File"); $config_notebook->append_page($connect_vbox, "Connection"); $config_notebook->append_page($table_waypoint, "Waypoint"); $config_notebook->append_page($vbox_uas, "UAS Settings"); #add button to the main window my $button_accept = Gtk2::Button->new_with_mnemonic("_Accept"); my $button_cancel = Gtk2::Button->new_with_mnemonic("_Cancel"); #pack them into the dialog window $table_config->attach_defaults($config_notebook, 0, 5, 0, 1); $table_config->attach_defaults($button_accept, 1, 2, 2, 3); $table_config->attach_defaults($button_cancel, 3, 4, 2, 3); $config_window->add($table_config); $config_window->show_all; #Button Functions $button_cancel->signal_connect('clicked' => sub {$config_window->d +estroy}); $button_accept->signal_connect('clicked' => sub { my @settings = ( + ($user = $entry_user->get_text), ($server = $entry_server->get_text) + ,($local_waypoint = $entry_local_waypoint->get_text), ($remote_waypo +int = $entry_remote_waypoint->get_text), ($local_uaspos = $entry_loca +l_uaspos->get_text), ($remote_uaspos = $entry_remote_uaspos->get_text +), ($local_uas = $entry_local_uas->get_text), ($remote_uas = $entry_r +emote_uas->get_text)); save_default(@settings); $config_window->destr +oy}); $button_file->signal_connect('clicked' => sub {my $chooser_file = +Gtk2::FileChooserDialog->new ( 'Open Config File...', undef, 'open')} +); return 1; } #Exit threads sub exit_threads { $die = 1; $thread_1->join; Gtk2->main_quit; return FALSE; } #save to the file default.cfg when apply is clicked sub save_default { open DEFAULT, '>', 'default.cfg' or die "Could not Open file!! $!" +; for (@_) { print DEFAULT "$_\n"; } close DEFAULT; } #thread 1; SFTP connection thread sub sftp{ $|++; while(1){ if ( $die == 1 ){ goto END }; if ( $sftp_connect == 1 ){ my $seconds = 120; my %args = (host=> $server, user=>$user, timeout=>$seconds +); my $sftp = Net::SFTP::Foreign->new(%args); for(;;){ if($sftp_connect == 0){ last} if($die == 1){ goto END }; } undef $sftp; $sftp_connect = 0; }else { sleep 1 } } END: }

Replies are listed 'Best First'.
Re^3: Gtk2: main window visibility
by zentara (Cardinal) on Jul 30, 2007 at 11:45 UTC
    I don't know how many times you've posted this code, or a variant, here or on the gtk2/perl maillist. First, if you want people to look at it, simplify it down to it's basics. When I tried your code, first I had to get 3 png images to test with, then I get an error about "no SFTP::Foreign, and finally it dosn't work as you claim. Your step(2) dosn't do anything on my machine, no interface appears. Finally your balloon pop ups just get in the way and are annoying.

    So please, reduce this to a minimal script, which uses pre-defined icons instead of the pngs, get rid of the annoying balloons, and the sftp stuff. Show us a script that simply concentrates on the problem you describe, i.e. popping up a window when clicking on a trayicon.

    I will be willing to bet, that in the process of simplifying this down to it's basics, you will find the glitch. But as it stands now, your script presents too many extraneous hurdles for us to casually spot your error.

    But just as a quick push in the right direction, you need to bind the interface sub to a left click.

    #events and timeouts $eventbox->signal_connect('button_release_event', \&click); # add a left click binding $eventbox->signal_connect('button_press_event', \&click); #handles tray clicks sub click { #left mouse button if ($_[1]->button == 1) { #Window show/hide controls if ( $current_controls != 0) { #&interface; if ( $visible == 0) { $visible = 1 } if ( $visible == 1) { $visible = 0 } } ################################ # call the interface window if a left click is done &interface; ############################## } #right mouse button elsif ($_[1]->button == 3) { &menu; } return 1; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      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 # } }
        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